Skip to content

Commit

Permalink
feat: Introduce way to get property value from dictionary and throw i…
Browse files Browse the repository at this point in the history
…f it does not exist
  • Loading branch information
jansav committed Mar 4, 2022
1 parent b909e72 commit 3707c78
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 4 deletions.
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"typescript": "^4.5.4",
"husky": "^7.0.4",
"@commitlint/cli": "^16.1.0",
"@commitlint/config-conventional": "^16.0.0"
"@commitlint/config-conventional": "^16.0.0",
"type-fest": "^2.12.0"
},
"bugs": {
"url": "https://github.com/ogre-works/ogre-tools/issues"
Expand Down
16 changes: 16 additions & 0 deletions packages/fp/ogre-tools-fp.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
declare module '@ogre-tools/fp' {
import { Get } from 'type-fest';

interface Pipeline {
<A, R1, R2, R3, R4, R5, R6, R7, R8, R9>(
arg: A,
Expand Down Expand Up @@ -76,4 +78,18 @@ declare module '@ogre-tools/fp' {
}

export const pipeline: Pipeline;

interface GetFrom {
<TDictionary, TPropertyPath extends string>(
dictionary: TDictionary,
propertyPath: TPropertyPath,
): Get<TDictionary, TPropertyPath>;

<TDictionary>(dictionary: TDictionary): <TPropertyPath extends string>(
propertyPath: TPropertyPath,
) => Get<TDictionary, TPropertyPath>;
}

export const getFrom: GetFrom;
export const getSafeFrom: GetFrom;
}
6 changes: 6 additions & 0 deletions packages/fp/src/getFrom/getFrom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import curry from 'lodash/fp/curry';
import get from 'lodash/fp/get';

export default curry((dictionary, propertyName) =>
get(propertyName, dictionary),
);
23 changes: 23 additions & 0 deletions packages/fp/src/getFrom/getFrom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import getSafeFrom from './getFrom';

describe('getSafeFrom', () => {
it('when getting existing value, returns value', () => {
const actual = getSafeFrom({ someProperty: 'some-value' })('someProperty');

expect(actual).toBe('some-value');
});

it('when getting non existing value, return undefined', () => {
const actual = getSafeFrom({})('someNonExistingProperty');

expect(actual).toBeUndefined();
});

it('when getting existing nested value, returns value', () => {
const actual = getSafeFrom({ someRoot: { someProperty: 'some-value' } })(
'someRoot.someProperty',
);

expect(actual).toBe('some-value');
});
});
11 changes: 11 additions & 0 deletions packages/fp/src/getSafeFrom/getSafeFrom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import curry from 'lodash/fp/curry';
import get from 'lodash/fp/get';
import has from 'lodash/fp/has';

export default curry((dictionary, propertyName) => {
if (!has(propertyName, dictionary)) {
throw new Error(`Tried to get unknown property "${propertyName}"`);
}

return get(propertyName, dictionary);
});
23 changes: 23 additions & 0 deletions packages/fp/src/getSafeFrom/getSafeFrom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import getSafeFrom from './getSafeFrom';

describe('getSafeFrom', () => {
it('when getting existing value, returns value', () => {
const actual = getSafeFrom({ someProperty: 'some-value' })('someProperty');

expect(actual).toBe('some-value');
});

it('when getting non existing value, throws', () => {
expect(() => {
getSafeFrom({})('someNonExistingProperty');
}).toThrow('Tried to get unknown property "someNonExistingProperty"');
});

it('when getting existing nested value, returns value', () => {
const actual = getSafeFrom({ someRoot: { someProperty: 'some-value' } })(
'someRoot.someProperty',
);

expect(actual).toBe('some-value');
});
});

0 comments on commit 3707c78

Please sign in to comment.