Skip to content

Commit

Permalink
feat: Delete lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasnisole authored and Thomas Nisole committed Jun 7, 2022
1 parent be917a9 commit 6a4242f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 37 deletions.
3 changes: 0 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ module.exports = {
preset: 'ts-jest',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec|spec-e2e))\\.ts?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'^lodash-es/(.*)$': '<rootDir>/node_modules/lodash/$1',
}
};
20 changes: 0 additions & 20 deletions package-lock.json

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

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
"rxjs": "^6.5.4",
"typescript": ">= 3.7.3"
},
"dependencies": {
"lodash-es": "^4.17.21"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.21",
"@types/lodash-es": "^4.17.4",
"@types/node": "^12.12.17",
"jest": "^26.5.4",
"lodash": "^4.17.21",
"rxjs": "^6.5.4",
"ts-jest": "^26.5.4",
"tslint": "^5.20.1",
Expand Down
2 changes: 1 addition & 1 deletion src/operator/if-not-null.operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MonoTypeOperatorFunction } from 'rxjs';
import { filter } from 'rxjs/operators';
import get from 'lodash/get';
import {get} from '../util';

/**
* filter != null value
Expand Down
3 changes: 1 addition & 2 deletions src/operator/join-array.operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { map } from 'rxjs/operators';
import { combineLatest, Observable } from 'rxjs';
import flatten from 'lodash/flatten';

/**
* Combines the latest values of source and each input array into a single array.
Expand All @@ -9,7 +8,7 @@ import flatten from 'lodash/flatten';
export function joinArray<I>(...input$: Observable<I[]>[]): any {
return (source$: Observable<I[]>) => {
return combineLatest([source$, ...input$]).pipe(
map((sources: I[][]) => flatten(sources)),
map((sources: I[][]) => sources.flat()),
);
};
}
4 changes: 2 additions & 2 deletions src/operator/wif.operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { switchMap } from 'rxjs/operators';
import { Observable, of, OperatorFunction } from 'rxjs';
import isFunction from 'lodash/isFunction';
import { isFunction } from '../util';

type WhenResult<I, O> = ((input: I) => Result<O>) | Result<O>;
type Result<O> = O | Observable<O>;
Expand All @@ -15,7 +15,7 @@ export function wif<I, T, E>(condition: (input: I) => boolean, whenTrue: WhenRes
return (source$: Observable<I>) => source$.pipe(
switchMap((input: I) => {
const route: WhenResult<I, T> | WhenResult<I, E> = condition(input) ? whenTrue : whenFalse;
const next: Result<T> | Result<E> = isFunction(route) ? route(input) : route;
const next: Result<T> | Result<E> = isFunction(route) ? (route as any)(input) : route;

return next instanceof Observable ? next : of(next);
})
Expand Down
66 changes: 66 additions & 0 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {get, isFunction} from './util';

describe('Util', () => {

describe('#isFunction', () => {

it('should return true if is function', () => {
expect(isFunction(function () {})).toBe(true);
});

it('should return true if is anonymous function', () => {
expect(isFunction(() => {})).toBe(true);
});

it('should return true if is class', () => {
expect(isFunction(class NotAFunction {})).toBe(true);
});

it('should return true if is regex', () => {
expect(isFunction(/abc/)).toBe(false);
});
});

describe('#get', () => {

const simpleObject = { a: { b: 2 } }
const complexObject = { a: [{ bar: { c: 3 } }] }
const falsyObject = { a: null, b: undefined, c: 0 }

it('a.b', () => {
expect(get(simpleObject, 'a.b')).toEqual(2);
});

it('a[0].bar.c', () => {
expect(get(complexObject, 'a[0].bar.c')).toEqual(3);
});

it('[\'a\', \'0\', \'bar\', \'c\']', () => {
expect(get(complexObject, ['a', '0', 'bar', 'c'])).toEqual(3);
});

it('a.bar.c with default', () => {
expect(get(simpleObject, 'a.bar.c', 'default')).toEqual('default');
});

it('a.bar.c with default', () => {
expect(get(complexObject, 'a.bar.c', 'default')).toEqual('default');
});

it('null', () => {
expect(get(complexObject, null)).toEqual(undefined);
});

it('a with default', () => {
expect(get(falsyObject, 'a', 'default')).toEqual(null);
});

it('b with default', () => {
expect(get(falsyObject, 'b', 'default')).toEqual('default');
});

it('c with default', () => {
expect(get(falsyObject, 'c', 'default')).toEqual(0);
});
});
})
17 changes: 17 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function isFunction(value: any): boolean {
return typeof value === 'function';
}

export function get(obj: any, path: string|string[], defValue?: any): any {
if (!path) {
return undefined;
}

const pathArray: string[] = Array.isArray(path) ? path : path.match(/([^[.\]])+/g);
const result: any = pathArray.reduce(
(prevObj: string, key: string) => prevObj && prevObj[key],
obj
);

return result === undefined ? defValue : result;
}
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
"declarationDir": "./dist",
"module": "esnext",
"sourceMap": true,
"lib": ["es2018", "dom"],
"lib": ["es2018", "es2019", "dom"],
"outDir": "./dist",
"target": "es5",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"typeRoots": ["node_modules/@types", "manual_typings"],
"paths": {
"lodash/*": ["node_modules/@types/lodash-es/*"]
},
"esModuleInterop": true
},
"typedocOptions": {
Expand Down

0 comments on commit 6a4242f

Please sign in to comment.