5.1.1 • Published 1 year ago

@smartlyio/safe-navigation v5.1.1

Weekly downloads
43
License
MIT
Repository
github
Last release
1 year ago

safe-navigation

type(script) and null safe navigation in json objects

get .$

Returns the value or undefined if any value on the path to it is undefined

// yarn ts-node examples/getter.ts
import safe from '../index';
import * as assert from 'assert';

interface A {
  a?: { b?: string };
}
const o: A = { a: { b: 'x' } };
assert(safe(o).a.b.$ === 'x');

The optional chaining also takes into account union types.

// yarn ts-node examples/unions.ts
import safe from '../index';
import * as assert from 'assert';

type A = { a: number } | { b: string };
const o: A = { a: 1 };
assert(safe(o).a.$ === 1);

set value .$set

Returns a new object with the target set to value if the path to value exists

// yarn ts-node examples/set.ts
import safe from '../index';
import * as assert from 'assert';

interface A {
  a?: { b?: string };
}
const o: A = { a: { b: 'old' } };
const newValue: A = safe(o).a.b.$set('new');
assert(safe(newValue).a.b.$ === 'new');

map value .$map

Maps the value at end of the path

// yarn ts-node examples/map.ts
import safe from '../index';
import * as assert from 'assert';

interface A {
  a?: { b?: string };
}
const o: A = { a: { b: 'old' } };
const newValue: A = safe(o).a.b.$map((n: any) => n + ' to new');
assert(safe(newValue).a.b.$ === 'old to new');

map with promises .$pmap

Returns a new object with the target mapped using a promise returning map function

// yarn ts-node examples/pmap.ts
import safe from '../index';
import * as assert from 'assert';

interface A {
  a?: { b?: string };
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async function () {
  const o: A = { a: { b: 'x' } };
  const result = await safe(o).a.b.$pmap(async value => 'got ' + value);
  assert(safe(o).a.b.$ === 'x');
  assert(safe(result).a.b.$ === 'got x');
})();
5.1.1

1 year ago

5.1.0

1 year ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.0-beta.0

5 years ago

3.0.0

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago