buildInitial

This commit is contained in:
Yanick Champoux 2021-09-27 23:11:13 -04:00
parent c96a6ea07c
commit 003a1309bd
4 changed files with 35 additions and 19 deletions

View File

@ -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",

15
src/buildInitial/index.js Normal file
View File

@ -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);
}

View File

@ -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;

12
src/buildInitial/test.js Normal file
View File

@ -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 },
});
});