diff --git a/package.json b/package.json index d3550a3..9c73895 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,13 @@ { + "type": "module", "dependencies": { + "@yanick/updeep": "../updeep", "lodash": "^4.17.15", + "lodash-es": "^4.17.21", "redux": "^4.0.5", "ts-action": "^11.0.0", "ts-node": "^8.6.2", - "@yanick/updeep": "^2.2.0" + "updeep": "^1.2.1" }, "devDependencies": { "@babel/cli": "^7.8.4", @@ -16,6 +19,7 @@ "@typescript-eslint/eslint-plugin": "^2.23.0", "@typescript-eslint/parser": "^2.23.0", "babel-jest": "^25.1.0", + "chai": "^4.3.4", "docsify": "^4.11.2", "docsify-cli": "^4.4.0", "docsify-tools": "^1.0.20", @@ -29,12 +33,13 @@ "promake": "^3.1.3", "sinon": "^9.0.1", "standard-version": "^8.0.0", - "tap": "^14.10.6", + "tap": "15", "ts-jest": "^25.2.1", "tsd": "^0.11.0", "typedoc": "0.17.7", "typedoc-plugin-markdown": "^2.2.17", - "typescript": "^4.2.0" + "typescript": "^4.2.0", + "uvu": "^0.5.1" }, "license": "MIT", "main": "dist/index.js", diff --git a/src/buildInitial/index.js b/src/buildInitial/index.js new file mode 100644 index 0000000..b387363 --- /dev/null +++ b/src/buildInitial/index.js @@ -0,0 +1,15 @@ +import { isPlainObject, mapValues } from 'lodash-es'; +import u from '@yanick/updeep'; + +export function buildInitial(initial, subduxes = {}) { + if (!isPlainObject(initial) && Object.keys(subduxes).length > 0) + throw new Error( + "can't have subduxes on a dux which state is not an object" + ); + + const subInitial = mapValues(subduxes, ({ initial }, key) => + key === '*' ? [] : initial + ); + + return u(initial, subInitial); +} diff --git a/src/buildInitial/index.ts b/src/buildInitial/index.ts deleted file mode 100644 index 3ecf225..0000000 --- a/src/buildInitial/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -import fp from 'lodash/fp'; -import u from '@yanick/updeep'; - -function buildInitial(initial: any, coduxes: any = [], subduxes: any = {}) { - if (!fp.isPlainObject(initial)) return initial; - - return fp.flow( - [ - u(fp.omit(['*'], subduxes)), - coduxes.map(i => u(i)), - u(initial), - ].flat() - )({}); -} - -export default buildInitial; diff --git a/src/buildInitial/test.js b/src/buildInitial/test.js new file mode 100644 index 0000000..5977827 --- /dev/null +++ b/src/buildInitial/test.js @@ -0,0 +1,12 @@ +import { test } from 'tap'; + +import { buildInitial } from './index.js'; + +test('basic', async (t) => { + t.same( + buildInitial({ a: 1 }, { b: { initial: { c: 2 } } }) + ,{ + a: 1, + b: { c: 3 }, + }); +});