diff --git a/jest.config.js b/jest.config.js index b03607e..15c8327 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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/(.*)$': '/node_modules/lodash/$1', - } }; diff --git a/package-lock.json b/package-lock.json index 4c2347c..3e37e9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1053,21 +1053,6 @@ "pretty-format": "^26.0.0" } }, - "@types/lodash": { - "version": "4.14.172", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.172.tgz", - "integrity": "sha512-/BHF5HAx3em7/KkzVKm3LrsD6HZAXuXO1AJZQ3cRRBZj4oHZDviWPYu0aEplAqDFNHZPW6d3G7KN+ONcCCC7pw==", - "dev": true - }, - "@types/lodash-es": { - "version": "4.17.4", - "resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.4.tgz", - "integrity": "sha512-BBz79DCJbD2CVYZH67MBeHZRX++HF+5p8Mo5MzjZi64Wac39S3diedJYHZtScbRVf4DjZyN6LzA0SB0zy+HSSQ==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, "@types/node": { "version": "12.12.17", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.17.tgz", @@ -4247,11 +4232,6 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, - "lodash-es": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", - "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" - }, "lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", diff --git a/package.json b/package.json index 3ae12ef..24b86ad 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/operator/if-not-null.operator.ts b/src/operator/if-not-null.operator.ts index 3ecc1b9..ac084bb 100644 --- a/src/operator/if-not-null.operator.ts +++ b/src/operator/if-not-null.operator.ts @@ -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 diff --git a/src/operator/join-array.operator.ts b/src/operator/join-array.operator.ts index fb52b82..35c1abe 100644 --- a/src/operator/join-array.operator.ts +++ b/src/operator/join-array.operator.ts @@ -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. @@ -9,7 +8,7 @@ import flatten from 'lodash/flatten'; export function joinArray(...input$: Observable[]): any { return (source$: Observable) => { return combineLatest([source$, ...input$]).pipe( - map((sources: I[][]) => flatten(sources)), + map((sources: I[][]) => sources.flat()), ); }; } diff --git a/src/operator/wif.operator.ts b/src/operator/wif.operator.ts index 7b6cb02..0f20258 100644 --- a/src/operator/wif.operator.ts +++ b/src/operator/wif.operator.ts @@ -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 = ((input: I) => Result) | Result; type Result = O | Observable; @@ -15,7 +15,7 @@ export function wif(condition: (input: I) => boolean, whenTrue: WhenRes return (source$: Observable) => source$.pipe( switchMap((input: I) => { const route: WhenResult | WhenResult = condition(input) ? whenTrue : whenFalse; - const next: Result | Result = isFunction(route) ? route(input) : route; + const next: Result | Result = isFunction(route) ? (route as any)(input) : route; return next instanceof Observable ? next : of(next); }) diff --git a/src/util.spec.ts b/src/util.spec.ts new file mode 100644 index 0000000..ffe15e4 --- /dev/null +++ b/src/util.spec.ts @@ -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); + }); + }); +}) diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..51c2a39 --- /dev/null +++ b/src/util.ts @@ -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; +} diff --git a/tsconfig.json b/tsconfig.json index 83e3ae0..59cc680 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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": {