From 11d058824f69a0d8d668c4b273cab673db671fda Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 3 Jan 2023 13:51:35 -0500 Subject: [PATCH] move to remeda --- .gitignore | 5 +- Taskfile.yaml | 18 +++ babel.config.js | 17 --- {types => commonjs}/constant.d.ts | 8 +- commonjs/constant.d.ts.map | 1 + commonjs/constant.js | 44 +++++++ commonjs/constant.js.map | 1 + commonjs/constant.test.d.ts | 2 + commonjs/constant.test.d.ts.map | 1 + commonjs/constant.test.js | 18 +++ commonjs/constant.test.js.map | 1 + {types => commonjs}/freeze.d.ts | 9 +- commonjs/freeze.d.ts.map | 1 + commonjs/freeze.js | 57 +++++++++ commonjs/freeze.js.map | 1 + commonjs/freeze.test.d.ts | 2 + commonjs/freeze.test.d.ts.map | 1 + commonjs/freeze.test.js | 61 +++++++++ commonjs/freeze.test.js.map | 1 + commonjs/if.d.ts | 7 ++ commonjs/if.d.ts.map | 1 + commonjs/if.js | 13 ++ commonjs/if.js.map | 1 + commonjs/if.test.d.ts | 2 + commonjs/if.test.d.ts.map | 1 + commonjs/if.test.js | 32 +++++ commonjs/if.test.js.map | 1 + commonjs/ifElse.d.ts | 8 ++ commonjs/ifElse.d.ts.map | 1 + commonjs/ifElse.js | 14 +++ commonjs/ifElse.js.map | 1 + commonjs/ifElse.test.d.ts | 2 + commonjs/ifElse.test.d.ts.map | 1 + commonjs/ifElse.test.js | 43 +++++++ commonjs/ifElse.test.js.map | 1 + commonjs/index.d.ts | 3 + commonjs/index.d.ts.map | 1 + commonjs/index.js | 46 +++++++ commonjs/index.js.map | 1 + commonjs/is.d.ts | 7 ++ commonjs/is.d.ts.map | 1 + commonjs/is.js | 21 ++++ commonjs/is.js.map | 1 + commonjs/is.test.d.ts | 2 + commonjs/is.test.d.ts.map | 1 + commonjs/is.test.js | 52 ++++++++ commonjs/is.test.js.map | 1 + commonjs/map.d.ts | 7 ++ commonjs/map.d.ts.map | 1 + commonjs/map.js | 24 ++++ commonjs/map.js.map | 1 + commonjs/map.test.d.ts | 2 + commonjs/map.test.d.ts.map | 1 + commonjs/map.test.js | 51 ++++++++ commonjs/map.test.js.map | 1 + commonjs/update.d.ts | 10 ++ commonjs/update.d.ts.map | 1 + commonjs/update.js | 78 ++++++++++++ commonjs/update.js.map | 1 + commonjs/updateIn.d.ts | 7 ++ commonjs/updateIn.d.ts.map | 1 + commonjs/updateIn.js | 26 ++++ commonjs/updateIn.js.map | 1 + commonjs/updateIn.test.d.ts | 2 + commonjs/updateIn.test.d.ts.map | 1 + commonjs/updateIn.test.js | 57 +++++++++ commonjs/updateIn.test.js.map | 1 + commonjs/updeep.bench.d.ts | 2 + commonjs/updeep.bench.d.ts.map | 1 + commonjs/updeep.bench.js | 15 +++ commonjs/updeep.bench.js.map | 1 + commonjs/updeep.test.d.ts | 2 + commonjs/updeep.test.d.ts.map | 1 + commonjs/updeep.test.js | 113 +++++++++++++++++ commonjs/updeep.test.js.map | 1 + commonjs/util/curry.d.ts | 7 ++ commonjs/util/curry.d.ts.map | 1 + commonjs/util/curry.js | 123 ++++++++++++++++++ commonjs/util/curry.js.map | 1 + commonjs/util/splitPath.d.ts | 2 + commonjs/util/splitPath.d.ts.map | 1 + commonjs/util/splitPath.js | 11 ++ commonjs/util/splitPath.js.map | 1 + commonjs/util/splitPath.test.d.ts | 2 + commonjs/util/splitPath.test.d.ts.map | 1 + commonjs/util/splitPath.test.js | 23 ++++ commonjs/util/splitPath.test.js.map | 1 + commonjs/wrap.d.ts | 3 + commonjs/wrap.d.ts.map | 1 + commonjs/wrap.js | 18 +++ commonjs/wrap.js.map | 1 + dist/constant.d.ts | 34 +++++ dist/constant.d.ts.map | 1 + dist/constant.js | 38 ++++++ dist/constant.js.map | 1 + dist/constant.test.d.ts | 2 + dist/constant.test.d.ts.map | 1 + dist/constant.test.js | 13 ++ dist/constant.test.js.map | 1 + dist/freeze.d.ts | 19 +++ dist/freeze.d.ts.map | 1 + dist/freeze.js | 55 ++++++++ dist/freeze.js.map | 1 + dist/freeze.test.d.ts | 2 + dist/freeze.test.d.ts.map | 1 + dist/freeze.test.js | 56 +++++++++ dist/freeze.test.js.map | 1 + dist/if.d.ts | 7 ++ dist/if.d.ts.map | 1 + dist/if.js | 8 ++ dist/if.js.map | 1 + dist/if.test.d.ts | 2 + dist/if.test.d.ts.map | 1 + dist/if.test.js | 27 ++++ dist/if.test.js.map | 1 + dist/ifElse.d.ts | 8 ++ dist/ifElse.d.ts.map | 1 + dist/ifElse.js | 9 ++ dist/ifElse.js.map | 1 + dist/ifElse.test.d.ts | 2 + dist/ifElse.test.d.ts.map | 1 + dist/ifElse.test.js | 38 ++++++ dist/ifElse.test.js.map | 1 + dist/index.d.ts | 3 + dist/index.d.ts.map | 1 + dist/index.js | 18 +++ dist/index.js.map | 1 + dist/is.d.ts | 7 ++ dist/is.d.ts.map | 1 + dist/is.js | 16 +++ dist/is.js.map | 1 + dist/is.test.d.ts | 2 + dist/is.test.d.ts.map | 1 + dist/is.test.js | 47 +++++++ dist/is.test.js.map | 1 + dist/map.d.ts | 7 ++ dist/map.d.ts.map | 1 + dist/map.js | 19 +++ dist/map.js.map | 1 + dist/map.test.d.ts | 2 + dist/map.test.d.ts.map | 1 + dist/map.test.js | 46 +++++++ dist/map.test.js.map | 1 + dist/update.d.ts | 10 ++ dist/update.d.ts.map | 1 + dist/update.js | 72 +++++++++++ dist/update.js.map | 1 + dist/updateIn.d.ts | 7 ++ dist/updateIn.d.ts.map | 1 + dist/updateIn.js | 21 ++++ dist/updateIn.js.map | 1 + dist/updateIn.test.d.ts | 2 + dist/updateIn.test.d.ts.map | 1 + dist/updateIn.test.js | 52 ++++++++ dist/updateIn.test.js.map | 1 + dist/updeep.bench.d.ts | 2 + dist/updeep.bench.d.ts.map | 1 + dist/updeep.bench.js | 10 ++ dist/updeep.bench.js.map | 1 + dist/updeep.test.d.ts | 2 + dist/updeep.test.d.ts.map | 1 + dist/updeep.test.js | 108 ++++++++++++++++ dist/updeep.test.js.map | 1 + dist/util/curry.d.ts | 7 ++ dist/util/curry.d.ts.map | 1 + dist/util/curry.js | 115 +++++++++++++++++ dist/util/curry.js.map | 1 + dist/util/splitPath.d.ts | 2 + dist/util/splitPath.d.ts.map | 1 + dist/util/splitPath.js | 8 ++ dist/util/splitPath.js.map | 1 + dist/util/splitPath.test.d.ts | 2 + dist/util/splitPath.test.d.ts.map | 1 + dist/util/splitPath.test.js | 18 +++ dist/util/splitPath.test.js.map | 1 + dist/wrap.d.ts | 3 + dist/wrap.d.ts.map | 1 + dist/wrap.js | 13 ++ dist/wrap.js.map | 1 + esm/constant.d.ts | 34 +++++ esm/constant.d.ts.map | 1 + esm/constant.js | 38 ++++++ esm/constant.js.map | 1 + esm/constant.test.d.ts | 2 + esm/constant.test.d.ts.map | 1 + esm/constant.test.js | 13 ++ esm/constant.test.js.map | 1 + esm/freeze.d.ts | 19 +++ esm/freeze.d.ts.map | 1 + esm/freeze.js | 55 ++++++++ esm/freeze.js.map | 1 + esm/freeze.test.d.ts | 2 + esm/freeze.test.d.ts.map | 1 + esm/freeze.test.js | 56 +++++++++ esm/freeze.test.js.map | 1 + esm/if.d.ts | 7 ++ esm/if.d.ts.map | 1 + esm/if.js | 8 ++ esm/if.js.map | 1 + esm/if.test.d.ts | 2 + esm/if.test.d.ts.map | 1 + esm/if.test.js | 27 ++++ esm/if.test.js.map | 1 + esm/ifElse.d.ts | 8 ++ esm/ifElse.d.ts.map | 1 + esm/ifElse.js | 9 ++ esm/ifElse.js.map | 1 + esm/ifElse.test.d.ts | 2 + esm/ifElse.test.d.ts.map | 1 + esm/ifElse.test.js | 38 ++++++ esm/ifElse.test.js.map | 1 + esm/index.d.ts | 3 + esm/index.d.ts.map | 1 + esm/index.js | 18 +++ esm/index.js.map | 1 + esm/is.d.ts | 7 ++ esm/is.d.ts.map | 1 + esm/is.js | 16 +++ esm/is.js.map | 1 + esm/is.test.d.ts | 2 + esm/is.test.d.ts.map | 1 + esm/is.test.js | 47 +++++++ esm/is.test.js.map | 1 + esm/map.d.ts | 7 ++ esm/map.d.ts.map | 1 + esm/map.js | 19 +++ esm/map.js.map | 1 + esm/map.test.d.ts | 2 + esm/map.test.d.ts.map | 1 + esm/map.test.js | 46 +++++++ esm/map.test.js.map | 1 + esm/update.d.ts | 10 ++ esm/update.d.ts.map | 1 + esm/update.js | 72 +++++++++++ esm/update.js.map | 1 + esm/updateIn.d.ts | 7 ++ esm/updateIn.d.ts.map | 1 + esm/updateIn.js | 21 ++++ esm/updateIn.js.map | 1 + esm/updateIn.test.d.ts | 2 + esm/updateIn.test.d.ts.map | 1 + esm/updateIn.test.js | 52 ++++++++ esm/updateIn.test.js.map | 1 + esm/updeep.bench.d.ts | 2 + esm/updeep.bench.d.ts.map | 1 + esm/updeep.bench.js | 10 ++ esm/updeep.bench.js.map | 1 + esm/updeep.test.d.ts | 2 + esm/updeep.test.d.ts.map | 1 + esm/updeep.test.js | 108 ++++++++++++++++ esm/updeep.test.js.map | 1 + esm/util/curry.d.ts | 7 ++ esm/util/curry.d.ts.map | 1 + esm/util/curry.js | 115 +++++++++++++++++ esm/util/curry.js.map | 1 + esm/util/splitPath.d.ts | 2 + esm/util/splitPath.d.ts.map | 1 + esm/util/splitPath.js | 8 ++ esm/util/splitPath.js.map | 1 + esm/util/splitPath.test.d.ts | 2 + esm/util/splitPath.test.d.ts.map | 1 + esm/util/splitPath.test.js | 18 +++ esm/util/splitPath.test.js.map | 1 + esm/wrap.d.ts | 3 + esm/wrap.d.ts.map | 1 + esm/wrap.js | 13 ++ esm/wrap.js.map | 1 + husky.config.js | 7 -- lib/freeze.js | 62 --------- lib/if.js | 6 - lib/ifElse.js | 12 -- lib/index.js | 32 ----- lib/is.js | 21 ---- lib/map.js | 33 ----- lib/omit.js | 8 -- lib/omitBy.js | 8 -- lib/reject.js | 11 -- lib/updateIn.js | 28 ----- lib/util/splitPath.js | 5 - lib/withDefault.js | 12 -- lib/wrap.js | 6 - lint-staged.config.js | 6 - package.json | 37 +++--- perf/index.js | 104 --------------- prettier.config.js | 12 -- prettier.config.mjs | 10 ++ src/constant.test.ts | 15 +++ lib/constant.js => src/constant.ts | 10 +- src/freeze.test.ts | 74 +++++++++++ src/freeze.ts | 62 +++++++++ src/if.test.ts | 33 +++++ src/if.ts | 15 +++ src/ifElse.test.ts | 45 +++++++ src/ifElse.ts | 19 +++ src/index.ts | 20 +++ src/is.test.ts | 58 +++++++++ src/is.ts | 28 +++++ src/map.test.ts | 58 +++++++++ src/map.ts | 29 +++++ lib/update.js => src/update.ts | 68 +++++----- src/updateIn.test.ts | 60 +++++++++ src/updateIn.ts | 34 +++++ src/updeep.bench.ts | 12 ++ src/updeep.test.ts | 155 +++++++++++++++++++++++ {lib => src}/util/curry.js | 0 src/util/splitPath.test.ts | 20 +++ src/util/splitPath.ts | 7 ++ src/wrap.ts | 17 +++ test/constant-spec.js | 17 --- test/freeze-spec.js | 73 ----------- test/if-spec.js | 47 ------- test/ifElse-spec.js | 45 ------- test/is-spec.js | 59 --------- test/map-spec.js | 75 ----------- test/omit-spec.js | 14 --- test/omitBy-spec.js | 18 --- test/reject-spec.js | 44 ------- test/updateIn-spec.js | 61 --------- test/updeep-spec.js | 174 -------------------------- test/util/splitPath-spec.js | 22 ---- test/withDefault-spec.js | 34 ----- tsconfig-cjs.json | 8 ++ tsconfig.json | 105 ++++++++++++++++ types/if.d.ts | 13 -- types/ifElse.d.ts | 13 -- types/index.d.ts | 30 ----- types/is.d.ts | 9 -- types/map.d.ts | 11 -- types/omit.d.ts | 7 -- types/omitBy.d.ts | 7 -- types/reject.d.ts | 9 -- types/tests.ts | 61 --------- types/tsconfig.json | 14 --- types/tslint.json | 11 -- types/types.d.ts | 14 --- types/update.d.ts | 32 ----- types/updateIn.d.ts | 11 -- types/util/curry.d.ts | 12 -- types/util/splitPath.d.ts | 2 - types/withDefault.d.ts | 8 -- types/wrap.d.ts | 2 - vitest.config.js | 5 + wallaby.conf.js | 16 --- 343 files changed, 4017 insertions(+), 1417 deletions(-) create mode 100644 Taskfile.yaml delete mode 100644 babel.config.js rename {types => commonjs}/constant.d.ts (68%) create mode 100644 commonjs/constant.d.ts.map create mode 100644 commonjs/constant.js create mode 100644 commonjs/constant.js.map create mode 100644 commonjs/constant.test.d.ts create mode 100644 commonjs/constant.test.d.ts.map create mode 100644 commonjs/constant.test.js create mode 100644 commonjs/constant.test.js.map rename {types => commonjs}/freeze.d.ts (50%) create mode 100644 commonjs/freeze.d.ts.map create mode 100644 commonjs/freeze.js create mode 100644 commonjs/freeze.js.map create mode 100644 commonjs/freeze.test.d.ts create mode 100644 commonjs/freeze.test.d.ts.map create mode 100644 commonjs/freeze.test.js create mode 100644 commonjs/freeze.test.js.map create mode 100644 commonjs/if.d.ts create mode 100644 commonjs/if.d.ts.map create mode 100644 commonjs/if.js create mode 100644 commonjs/if.js.map create mode 100644 commonjs/if.test.d.ts create mode 100644 commonjs/if.test.d.ts.map create mode 100644 commonjs/if.test.js create mode 100644 commonjs/if.test.js.map create mode 100644 commonjs/ifElse.d.ts create mode 100644 commonjs/ifElse.d.ts.map create mode 100644 commonjs/ifElse.js create mode 100644 commonjs/ifElse.js.map create mode 100644 commonjs/ifElse.test.d.ts create mode 100644 commonjs/ifElse.test.d.ts.map create mode 100644 commonjs/ifElse.test.js create mode 100644 commonjs/ifElse.test.js.map create mode 100644 commonjs/index.d.ts create mode 100644 commonjs/index.d.ts.map create mode 100644 commonjs/index.js create mode 100644 commonjs/index.js.map create mode 100644 commonjs/is.d.ts create mode 100644 commonjs/is.d.ts.map create mode 100644 commonjs/is.js create mode 100644 commonjs/is.js.map create mode 100644 commonjs/is.test.d.ts create mode 100644 commonjs/is.test.d.ts.map create mode 100644 commonjs/is.test.js create mode 100644 commonjs/is.test.js.map create mode 100644 commonjs/map.d.ts create mode 100644 commonjs/map.d.ts.map create mode 100644 commonjs/map.js create mode 100644 commonjs/map.js.map create mode 100644 commonjs/map.test.d.ts create mode 100644 commonjs/map.test.d.ts.map create mode 100644 commonjs/map.test.js create mode 100644 commonjs/map.test.js.map create mode 100644 commonjs/update.d.ts create mode 100644 commonjs/update.d.ts.map create mode 100644 commonjs/update.js create mode 100644 commonjs/update.js.map create mode 100644 commonjs/updateIn.d.ts create mode 100644 commonjs/updateIn.d.ts.map create mode 100644 commonjs/updateIn.js create mode 100644 commonjs/updateIn.js.map create mode 100644 commonjs/updateIn.test.d.ts create mode 100644 commonjs/updateIn.test.d.ts.map create mode 100644 commonjs/updateIn.test.js create mode 100644 commonjs/updateIn.test.js.map create mode 100644 commonjs/updeep.bench.d.ts create mode 100644 commonjs/updeep.bench.d.ts.map create mode 100644 commonjs/updeep.bench.js create mode 100644 commonjs/updeep.bench.js.map create mode 100644 commonjs/updeep.test.d.ts create mode 100644 commonjs/updeep.test.d.ts.map create mode 100644 commonjs/updeep.test.js create mode 100644 commonjs/updeep.test.js.map create mode 100644 commonjs/util/curry.d.ts create mode 100644 commonjs/util/curry.d.ts.map create mode 100644 commonjs/util/curry.js create mode 100644 commonjs/util/curry.js.map create mode 100644 commonjs/util/splitPath.d.ts create mode 100644 commonjs/util/splitPath.d.ts.map create mode 100644 commonjs/util/splitPath.js create mode 100644 commonjs/util/splitPath.js.map create mode 100644 commonjs/util/splitPath.test.d.ts create mode 100644 commonjs/util/splitPath.test.d.ts.map create mode 100644 commonjs/util/splitPath.test.js create mode 100644 commonjs/util/splitPath.test.js.map create mode 100644 commonjs/wrap.d.ts create mode 100644 commonjs/wrap.d.ts.map create mode 100644 commonjs/wrap.js create mode 100644 commonjs/wrap.js.map create mode 100644 dist/constant.d.ts create mode 100644 dist/constant.d.ts.map create mode 100644 dist/constant.js create mode 100644 dist/constant.js.map create mode 100644 dist/constant.test.d.ts create mode 100644 dist/constant.test.d.ts.map create mode 100644 dist/constant.test.js create mode 100644 dist/constant.test.js.map create mode 100644 dist/freeze.d.ts create mode 100644 dist/freeze.d.ts.map create mode 100644 dist/freeze.js create mode 100644 dist/freeze.js.map create mode 100644 dist/freeze.test.d.ts create mode 100644 dist/freeze.test.d.ts.map create mode 100644 dist/freeze.test.js create mode 100644 dist/freeze.test.js.map create mode 100644 dist/if.d.ts create mode 100644 dist/if.d.ts.map create mode 100644 dist/if.js create mode 100644 dist/if.js.map create mode 100644 dist/if.test.d.ts create mode 100644 dist/if.test.d.ts.map create mode 100644 dist/if.test.js create mode 100644 dist/if.test.js.map create mode 100644 dist/ifElse.d.ts create mode 100644 dist/ifElse.d.ts.map create mode 100644 dist/ifElse.js create mode 100644 dist/ifElse.js.map create mode 100644 dist/ifElse.test.d.ts create mode 100644 dist/ifElse.test.d.ts.map create mode 100644 dist/ifElse.test.js create mode 100644 dist/ifElse.test.js.map create mode 100644 dist/index.d.ts create mode 100644 dist/index.d.ts.map create mode 100644 dist/index.js create mode 100644 dist/index.js.map create mode 100644 dist/is.d.ts create mode 100644 dist/is.d.ts.map create mode 100644 dist/is.js create mode 100644 dist/is.js.map create mode 100644 dist/is.test.d.ts create mode 100644 dist/is.test.d.ts.map create mode 100644 dist/is.test.js create mode 100644 dist/is.test.js.map create mode 100644 dist/map.d.ts create mode 100644 dist/map.d.ts.map create mode 100644 dist/map.js create mode 100644 dist/map.js.map create mode 100644 dist/map.test.d.ts create mode 100644 dist/map.test.d.ts.map create mode 100644 dist/map.test.js create mode 100644 dist/map.test.js.map create mode 100644 dist/update.d.ts create mode 100644 dist/update.d.ts.map create mode 100644 dist/update.js create mode 100644 dist/update.js.map create mode 100644 dist/updateIn.d.ts create mode 100644 dist/updateIn.d.ts.map create mode 100644 dist/updateIn.js create mode 100644 dist/updateIn.js.map create mode 100644 dist/updateIn.test.d.ts create mode 100644 dist/updateIn.test.d.ts.map create mode 100644 dist/updateIn.test.js create mode 100644 dist/updateIn.test.js.map create mode 100644 dist/updeep.bench.d.ts create mode 100644 dist/updeep.bench.d.ts.map create mode 100644 dist/updeep.bench.js create mode 100644 dist/updeep.bench.js.map create mode 100644 dist/updeep.test.d.ts create mode 100644 dist/updeep.test.d.ts.map create mode 100644 dist/updeep.test.js create mode 100644 dist/updeep.test.js.map create mode 100644 dist/util/curry.d.ts create mode 100644 dist/util/curry.d.ts.map create mode 100644 dist/util/curry.js create mode 100644 dist/util/curry.js.map create mode 100644 dist/util/splitPath.d.ts create mode 100644 dist/util/splitPath.d.ts.map create mode 100644 dist/util/splitPath.js create mode 100644 dist/util/splitPath.js.map create mode 100644 dist/util/splitPath.test.d.ts create mode 100644 dist/util/splitPath.test.d.ts.map create mode 100644 dist/util/splitPath.test.js create mode 100644 dist/util/splitPath.test.js.map create mode 100644 dist/wrap.d.ts create mode 100644 dist/wrap.d.ts.map create mode 100644 dist/wrap.js create mode 100644 dist/wrap.js.map create mode 100644 esm/constant.d.ts create mode 100644 esm/constant.d.ts.map create mode 100644 esm/constant.js create mode 100644 esm/constant.js.map create mode 100644 esm/constant.test.d.ts create mode 100644 esm/constant.test.d.ts.map create mode 100644 esm/constant.test.js create mode 100644 esm/constant.test.js.map create mode 100644 esm/freeze.d.ts create mode 100644 esm/freeze.d.ts.map create mode 100644 esm/freeze.js create mode 100644 esm/freeze.js.map create mode 100644 esm/freeze.test.d.ts create mode 100644 esm/freeze.test.d.ts.map create mode 100644 esm/freeze.test.js create mode 100644 esm/freeze.test.js.map create mode 100644 esm/if.d.ts create mode 100644 esm/if.d.ts.map create mode 100644 esm/if.js create mode 100644 esm/if.js.map create mode 100644 esm/if.test.d.ts create mode 100644 esm/if.test.d.ts.map create mode 100644 esm/if.test.js create mode 100644 esm/if.test.js.map create mode 100644 esm/ifElse.d.ts create mode 100644 esm/ifElse.d.ts.map create mode 100644 esm/ifElse.js create mode 100644 esm/ifElse.js.map create mode 100644 esm/ifElse.test.d.ts create mode 100644 esm/ifElse.test.d.ts.map create mode 100644 esm/ifElse.test.js create mode 100644 esm/ifElse.test.js.map create mode 100644 esm/index.d.ts create mode 100644 esm/index.d.ts.map create mode 100644 esm/index.js create mode 100644 esm/index.js.map create mode 100644 esm/is.d.ts create mode 100644 esm/is.d.ts.map create mode 100644 esm/is.js create mode 100644 esm/is.js.map create mode 100644 esm/is.test.d.ts create mode 100644 esm/is.test.d.ts.map create mode 100644 esm/is.test.js create mode 100644 esm/is.test.js.map create mode 100644 esm/map.d.ts create mode 100644 esm/map.d.ts.map create mode 100644 esm/map.js create mode 100644 esm/map.js.map create mode 100644 esm/map.test.d.ts create mode 100644 esm/map.test.d.ts.map create mode 100644 esm/map.test.js create mode 100644 esm/map.test.js.map create mode 100644 esm/update.d.ts create mode 100644 esm/update.d.ts.map create mode 100644 esm/update.js create mode 100644 esm/update.js.map create mode 100644 esm/updateIn.d.ts create mode 100644 esm/updateIn.d.ts.map create mode 100644 esm/updateIn.js create mode 100644 esm/updateIn.js.map create mode 100644 esm/updateIn.test.d.ts create mode 100644 esm/updateIn.test.d.ts.map create mode 100644 esm/updateIn.test.js create mode 100644 esm/updateIn.test.js.map create mode 100644 esm/updeep.bench.d.ts create mode 100644 esm/updeep.bench.d.ts.map create mode 100644 esm/updeep.bench.js create mode 100644 esm/updeep.bench.js.map create mode 100644 esm/updeep.test.d.ts create mode 100644 esm/updeep.test.d.ts.map create mode 100644 esm/updeep.test.js create mode 100644 esm/updeep.test.js.map create mode 100644 esm/util/curry.d.ts create mode 100644 esm/util/curry.d.ts.map create mode 100644 esm/util/curry.js create mode 100644 esm/util/curry.js.map create mode 100644 esm/util/splitPath.d.ts create mode 100644 esm/util/splitPath.d.ts.map create mode 100644 esm/util/splitPath.js create mode 100644 esm/util/splitPath.js.map create mode 100644 esm/util/splitPath.test.d.ts create mode 100644 esm/util/splitPath.test.d.ts.map create mode 100644 esm/util/splitPath.test.js create mode 100644 esm/util/splitPath.test.js.map create mode 100644 esm/wrap.d.ts create mode 100644 esm/wrap.d.ts.map create mode 100644 esm/wrap.js create mode 100644 esm/wrap.js.map delete mode 100644 husky.config.js delete mode 100644 lib/freeze.js delete mode 100644 lib/if.js delete mode 100644 lib/ifElse.js delete mode 100644 lib/index.js delete mode 100644 lib/is.js delete mode 100644 lib/map.js delete mode 100644 lib/omit.js delete mode 100644 lib/omitBy.js delete mode 100644 lib/reject.js delete mode 100644 lib/updateIn.js delete mode 100644 lib/util/splitPath.js delete mode 100644 lib/withDefault.js delete mode 100644 lib/wrap.js delete mode 100644 lint-staged.config.js delete mode 100644 perf/index.js delete mode 100644 prettier.config.js create mode 100644 prettier.config.mjs create mode 100644 src/constant.test.ts rename lib/constant.js => src/constant.ts (84%) create mode 100644 src/freeze.test.ts create mode 100644 src/freeze.ts create mode 100644 src/if.test.ts create mode 100644 src/if.ts create mode 100644 src/ifElse.test.ts create mode 100644 src/ifElse.ts create mode 100644 src/index.ts create mode 100644 src/is.test.ts create mode 100644 src/is.ts create mode 100644 src/map.test.ts create mode 100644 src/map.ts rename lib/update.js => src/update.ts (55%) create mode 100644 src/updateIn.test.ts create mode 100644 src/updateIn.ts create mode 100644 src/updeep.bench.ts create mode 100644 src/updeep.test.ts rename {lib => src}/util/curry.js (100%) create mode 100644 src/util/splitPath.test.ts create mode 100644 src/util/splitPath.ts create mode 100644 src/wrap.ts delete mode 100644 test/constant-spec.js delete mode 100644 test/freeze-spec.js delete mode 100644 test/if-spec.js delete mode 100644 test/ifElse-spec.js delete mode 100644 test/is-spec.js delete mode 100644 test/map-spec.js delete mode 100644 test/omit-spec.js delete mode 100644 test/omitBy-spec.js delete mode 100644 test/reject-spec.js delete mode 100644 test/updateIn-spec.js delete mode 100644 test/updeep-spec.js delete mode 100644 test/util/splitPath-spec.js delete mode 100644 test/withDefault-spec.js create mode 100644 tsconfig-cjs.json create mode 100644 tsconfig.json delete mode 100644 types/if.d.ts delete mode 100644 types/ifElse.d.ts delete mode 100644 types/index.d.ts delete mode 100644 types/is.d.ts delete mode 100644 types/map.d.ts delete mode 100644 types/omit.d.ts delete mode 100644 types/omitBy.d.ts delete mode 100644 types/reject.d.ts delete mode 100644 types/tests.ts delete mode 100644 types/tsconfig.json delete mode 100644 types/tslint.json delete mode 100644 types/types.d.ts delete mode 100644 types/update.d.ts delete mode 100644 types/updateIn.d.ts delete mode 100644 types/util/curry.d.ts delete mode 100644 types/util/splitPath.d.ts delete mode 100644 types/withDefault.d.ts delete mode 100644 types/wrap.d.ts create mode 100644 vitest.config.js delete mode 100644 wallaby.conf.js diff --git a/.gitignore b/.gitignore index 9c62828..103b1c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ node_modules coverage -dist +pnpm-lock.yaml +tsconfig*.tsbuildinfo +lib +types diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..de4c5a3 --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,18 @@ +# https://taskfile.dev + +version: "3" + +vars: + GREETING: Hello, World! + +tasks: + build: + cmds: + - rm -fr types/ esm/ commonjs/ + - tsc -p tsconfig.json + - tsc -p tsconfig-cjs.json + - tsc -p tsconfig-types.json + default: + cmds: + - echo "{{.GREETING}}" + silent: true diff --git a/babel.config.js b/babel.config.js deleted file mode 100644 index 7228df3..0000000 --- a/babel.config.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = (api) => { - api.cache(true) - return { - presets: [ - [ - '@babel/preset-env', - { - loose: true, - targets: { - browsers: ['last 2 versions', 'IE >= 9'], - }, - }, - ], - '@babel/preset-typescript', - ], - } -} diff --git a/types/constant.d.ts b/commonjs/constant.d.ts similarity index 68% rename from types/constant.d.ts rename to commonjs/constant.d.ts index 9a80a45..0e17823 100644 --- a/types/constant.d.ts +++ b/commonjs/constant.d.ts @@ -3,7 +3,11 @@ * * Useful for replacing an object outright rather than merging it. * + * @function + * @sig a -> (* -> a) * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. * * @example * var alwaysFour = u.constant(4); @@ -26,5 +30,5 @@ * * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); */ -export declare function constant(value: A): (...args: any[]) => A; -export default constant; +export default function constant(value: T): () => Readonly; +//# sourceMappingURL=constant.d.ts.map \ No newline at end of file diff --git a/commonjs/constant.d.ts.map b/commonjs/constant.d.ts.map new file mode 100644 index 0000000..5a1fb8b --- /dev/null +++ b/commonjs/constant.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,qBAG3C"} \ No newline at end of file diff --git a/commonjs/constant.js b/commonjs/constant.js new file mode 100644 index 0000000..1547754 --- /dev/null +++ b/commonjs/constant.js @@ -0,0 +1,44 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const freeze_js_1 = __importDefault(require("./freeze.js")); +/** + * Returns a function that always returns the supplied value. + * + * Useful for replacing an object outright rather than merging it. + * + * @function + * @sig a -> (* -> a) + * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. + * + * @example + * var alwaysFour = u.constant(4); + * expect(alwaysFour(32)).toEqual(4); + * + * @example + * var user = { + * name: 'Mitch', + * favorites: { + * band: 'Nirvana', + * movie: 'The Matrix' + * } + * }; + * + * var newFavorites = { + * band: 'Coldplay' + * }; + * + * var result = u({ favorites: u.constant(newFavorites) }, user); + * + * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); + */ +function constant(value) { + const frozen = (0, freeze_js_1.default)(value); + return () => frozen; +} +exports.default = constant; +//# sourceMappingURL=constant.js.map \ No newline at end of file diff --git a/commonjs/constant.js.map b/commonjs/constant.js.map new file mode 100644 index 0000000..3b5b2bc --- /dev/null +++ b/commonjs/constant.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.js","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":";;;;;AAAA,4DAAiC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,SAAwB,QAAQ,CAAI,KAAQ;IAC1C,MAAM,MAAM,GAAG,IAAA,mBAAM,EAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC;AAHD,2BAGC"} \ No newline at end of file diff --git a/commonjs/constant.test.d.ts b/commonjs/constant.test.d.ts new file mode 100644 index 0000000..7a91645 --- /dev/null +++ b/commonjs/constant.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=constant.test.d.ts.map \ No newline at end of file diff --git a/commonjs/constant.test.d.ts.map b/commonjs/constant.test.d.ts.map new file mode 100644 index 0000000..9efc588 --- /dev/null +++ b/commonjs/constant.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.d.ts","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/constant.test.js b/commonjs/constant.test.js new file mode 100644 index 0000000..f5653fb --- /dev/null +++ b/commonjs/constant.test.js @@ -0,0 +1,18 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const constant_js_1 = __importDefault(require("./constant.js")); +(0, vitest_1.test)("returns what it is given... constantly", () => { + const func = (0, constant_js_1.default)(4); + (0, vitest_1.expect)(func()).to.equal(4); + (0, vitest_1.expect)(func("hi")).to.equal(4); + (0, vitest_1.expect)(func("hi", 8)).to.equal(4); + (0, vitest_1.expect)(func(4)).to.equal(4); +}); +(0, vitest_1.test)("freezes the result", () => { + (0, vitest_1.expect)(Object.isFrozen((0, constant_js_1.default)({})())).toBeTruthy(); +}); +//# sourceMappingURL=constant.test.js.map \ No newline at end of file diff --git a/commonjs/constant.test.js.map b/commonjs/constant.test.js.map new file mode 100644 index 0000000..6b10fb5 --- /dev/null +++ b/commonjs/constant.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.js","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAAgD;AAChD,gEAAqC;AAErC,IAAA,aAAI,EAAC,wCAAwC,EAAE,GAAG,EAAE;IAChD,MAAM,IAAI,GAAQ,IAAA,qBAAQ,EAAC,CAAC,CAAC,CAAC;IAE9B,IAAA,eAAM,EAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,IAAA,eAAM,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC/B,IAAA,eAAM,EAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClC,IAAA,eAAM,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,IAAA,aAAI,EAAC,oBAAoB,EAAE,GAAG,EAAE;IAC5B,IAAA,eAAM,EAAC,MAAM,CAAC,QAAQ,CAAC,IAAA,qBAAQ,EAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AACzD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/types/freeze.d.ts b/commonjs/freeze.d.ts similarity index 50% rename from types/freeze.d.ts rename to commonjs/freeze.d.ts index e21475a..7ae399c 100644 --- a/types/freeze.d.ts +++ b/commonjs/freeze.d.ts @@ -7,6 +7,13 @@ * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object * without freezing. * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. */ -declare function freeze(object: O): O; +declare function freeze(object: T): Readonly; export default freeze; +//# sourceMappingURL=freeze.d.ts.map \ No newline at end of file diff --git a/commonjs/freeze.d.ts.map b/commonjs/freeze.d.ts.map new file mode 100644 index 0000000..6057a76 --- /dev/null +++ b/commonjs/freeze.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.d.ts","sourceRoot":"","sources":["../src/freeze.ts"],"names":[],"mappings":"AAyBA;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAkBzC;AAED,eAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/commonjs/freeze.js b/commonjs/freeze.js new file mode 100644 index 0000000..c50d4f8 --- /dev/null +++ b/commonjs/freeze.js @@ -0,0 +1,57 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +function isFreezable(object) { + if (object === null) + return false; + if (object instanceof RegExp) + return false; + if (object instanceof ArrayBuffer) + return false; + return Array.isArray(object) || typeof object === "object"; +} +function needsFreezing(object) { + return isFreezable(object) && !Object.isFrozen(object); +} +function recur(object) { + Object.freeze(object); + Object.keys(object).forEach((key) => { + const value = object[key]; + if (needsFreezing(value)) { + recur(value); + } + }); + return object; +} +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +function freeze(object) { + if (typeof process === "undefined") { + return object; + } + if (process.env.NODE_ENV === "production") { + return object; + } + if (process.env.UPDEEP_MODE === "dangerously_never_freeze") { + return object; + } + if (needsFreezing(object)) { + recur(object); + } + return object; +} +exports.default = freeze; +//# sourceMappingURL=freeze.js.map \ No newline at end of file diff --git a/commonjs/freeze.js.map b/commonjs/freeze.js.map new file mode 100644 index 0000000..4af9f05 --- /dev/null +++ b/commonjs/freeze.js.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.js","sourceRoot":"","sources":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B,CAAC;AAED,kBAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/commonjs/freeze.test.d.ts b/commonjs/freeze.test.d.ts new file mode 100644 index 0000000..ecc5984 --- /dev/null +++ b/commonjs/freeze.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=freeze.test.d.ts.map \ No newline at end of file diff --git a/commonjs/freeze.test.d.ts.map b/commonjs/freeze.test.d.ts.map new file mode 100644 index 0000000..07a38c1 --- /dev/null +++ b/commonjs/freeze.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.test.d.ts","sourceRoot":"","sources":["../src/freeze.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/freeze.test.js b/commonjs/freeze.test.js new file mode 100644 index 0000000..61e2d76 --- /dev/null +++ b/commonjs/freeze.test.js @@ -0,0 +1,61 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const freeze_js_1 = __importDefault(require("./freeze.js")); +(0, vitest_1.describe)("freeze", () => { + (0, vitest_1.afterEach)(() => { + delete process.env.NODE_ENV; + }); + (0, vitest_1.test)("freezes objects", () => { + const object = {}; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object)).to.be.true; + }); + (0, vitest_1.test)("freezes nested objects", () => { + const object = { foo: { bar: 3 } }; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object.foo)).to.be.true; + }); + (0, vitest_1.test)("freezes nested arrays", () => { + const object = [[0]]; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object)).to.be.true; + (0, vitest_1.expect)(Object.isFrozen(object[0])).to.be.true; + }); + (0, vitest_1.test)("ignores functions", () => { + const object = { foo: () => 1 }; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object.foo)).to.be.false; + }); + (0, vitest_1.test)("ignores regexps", () => { + const object = { foo: /\d/ }; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object.foo)).to.be.false; + }); + (0, vitest_1.test)("does not freeze children if the parent is already frozen", () => { + const object = { foo: {} }; + Object.freeze(object); + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object.foo)).to.be.false; + }); + (0, vitest_1.test)("does not freeze in production", () => { + process.env.NODE_ENV = "production"; + const object = {}; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object)).to.be.false; + }); + (0, vitest_1.test)("handles null objects", () => { + const object = { foo: null }; + (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(Object.isFrozen(object)).to.be.true; + }); + (0, vitest_1.test)("returns the same object", () => { + const object = {}; + const result = (0, freeze_js_1.default)(object); + (0, vitest_1.expect)(result).to.equal(object); + }); +}); +//# sourceMappingURL=freeze.test.js.map \ No newline at end of file diff --git a/commonjs/freeze.test.js.map b/commonjs/freeze.test.js.map new file mode 100644 index 0000000..dc3a672 --- /dev/null +++ b/commonjs/freeze.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.test.js","sourceRoot":"","sources":["../src/freeze.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAA2D;AAE3D,4DAAiC;AAEjC,IAAA,iBAAQ,EAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAA,kBAAS,EAAC,GAAG,EAAE;QACb,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,iBAAiB,EAAE,GAAG,EAAE;QAC3B,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,IAAA,mBAAM,EAAC,MAAM,CAAC,CAAC;QAEf,IAAA,eAAM,EAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAC7C,CAAC,CAA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CAAC,CAAC;QAC9B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/if.d.ts b/commonjs/if.d.ts new file mode 100644 index 0000000..f11eafc --- /dev/null +++ b/commonjs/if.d.ts @@ -0,0 +1,7 @@ +interface If { + (object: any, predicate: any, trueUpdates: any): unknown; + (predicate: any, trueUpdates: any): (unknown: any) => unknown; +} +declare const _default: If; +export default _default; +//# sourceMappingURL=if.d.ts.map \ No newline at end of file diff --git a/commonjs/if.d.ts.map b/commonjs/if.d.ts.map new file mode 100644 index 0000000..af7f764 --- /dev/null +++ b/commonjs/if.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.d.ts","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":"AAKA,UAAU,EAAE;IACV,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,OAAO,CAAC;IAC1C,CAAC,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CAChD;;AAMD,wBAA+B"} \ No newline at end of file diff --git a/commonjs/if.js b/commonjs/if.js new file mode 100644 index 0000000..532dcd0 --- /dev/null +++ b/commonjs/if.js @@ -0,0 +1,13 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const remeda_1 = require("remeda"); +const ifElse_js_1 = __importDefault(require("./ifElse.js")); +const wrap_js_1 = __importDefault(require("./wrap.js")); +function _if(object, predicate, trueUpdates) { + return (0, ifElse_js_1.default)(object, predicate, trueUpdates, remeda_1.identity); +} +exports.default = (0, wrap_js_1.default)(_if); +//# sourceMappingURL=if.js.map \ No newline at end of file diff --git a/commonjs/if.js.map b/commonjs/if.js.map new file mode 100644 index 0000000..2b130a3 --- /dev/null +++ b/commonjs/if.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.js","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":";;;;;AAAA,mCAAkC;AAElC,4DAAiC;AACjC,wDAA6B;AAO7B,SAAS,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW;IACzC,OAAO,IAAA,mBAAM,EAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,iBAAQ,CAAC,CAAC;AAC1D,CAAC;AAED,kBAAe,IAAA,iBAAI,EAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/commonjs/if.test.d.ts b/commonjs/if.test.d.ts new file mode 100644 index 0000000..d034164 --- /dev/null +++ b/commonjs/if.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=if.test.d.ts.map \ No newline at end of file diff --git a/commonjs/if.test.d.ts.map b/commonjs/if.test.d.ts.map new file mode 100644 index 0000000..1d2dd89 --- /dev/null +++ b/commonjs/if.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.d.ts","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/if.test.js b/commonjs/if.test.js new file mode 100644 index 0000000..6737398 --- /dev/null +++ b/commonjs/if.test.js @@ -0,0 +1,32 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const if_js_1 = __importDefault(require("./if.js")); +(0, vitest_1.describe)("if", () => { + (0, vitest_1.test)("does not update if the predicate is false", () => { + const object = { a: 0 }; + let result = (0, if_js_1.default)(false, { b: 1 }, object); + (0, vitest_1.expect)(result).to.eql(object); + result = (0, if_js_1.default)(0, false, 1); + (0, vitest_1.expect)(result).to.eql(0); + }); + (0, vitest_1.test)("does update if the predicate is true", () => { + const object = { a: 0 }; + const result = (0, if_js_1.default)(true, { b: 1 })(object); + (0, vitest_1.expect)(result).to.eql({ a: 0, b: 1 }); + }); + (0, vitest_1.test)("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = (0, if_js_1.default)(aIsThree, { b: 1 }, object); + (0, vitest_1.expect)(result).to.eql({ a: 0 }); + }); + (0, vitest_1.test)("freezes the result", () => { + (0, vitest_1.expect)(Object.isFrozen((0, if_js_1.default)(true, {})({}))).to.be.true; + (0, vitest_1.expect)(Object.isFrozen((0, if_js_1.default)(false, {})({}))).to.be.true; + }); +}); +//# sourceMappingURL=if.test.js.map \ No newline at end of file diff --git a/commonjs/if.test.js.map b/commonjs/if.test.js.map new file mode 100644 index 0000000..9686fbf --- /dev/null +++ b/commonjs/if.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.js","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAAgD;AAEhD,oDAA0B;AAE1B,IAAA,iBAAQ,EAAC,IAAI,EAAE,GAAG,EAAE;IAClB,IAAA,aAAI,EAAC,2CAA2C,EAAE,GAAG,EAAE;QACrD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,IAAI,MAAM,GAAG,IAAA,eAAG,EAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QAC1C,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAE9B,MAAM,GAAG,IAAA,eAAG,EAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QAC1B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,sCAAsC,EAAE,GAAG,EAAE;QAChD,MAAM,MAAM,GA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.d.ts new file mode 100644 index 0000000..e748197 --- /dev/null +++ b/commonjs/ifElse.d.ts @@ -0,0 +1,8 @@ +type Predicate = ((source: any) => boolean) | boolean; +interface IfElse { + (object: any, predicate: Predicate, trueUpdates: any, falseUpdates: any): unknown; + (predicate: Predicate, trueUpdates: any, falseUpdates: any): (unknown: any) => unknown; +} +declare const _default: IfElse; +export default _default; +//# sourceMappingURL=ifElse.d.ts.map \ No newline at end of file diff --git a/commonjs/ifElse.d.ts.map b/commonjs/ifElse.d.ts.map new file mode 100644 index 0000000..c085b20 --- /dev/null +++ b/commonjs/ifElse.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.d.ts","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":"AAWA,KAAK,SAAS,GAAG,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,OAAO,CAAC;AAEtD,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,OAAO,CAAC;IACnE,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CACzE;;AAED,wBAA4C"} \ No newline at end of file diff --git a/commonjs/ifElse.js b/commonjs/ifElse.js new file mode 100644 index 0000000..9232db9 --- /dev/null +++ b/commonjs/ifElse.js @@ -0,0 +1,14 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const update_js_1 = __importDefault(require("./update.js")); +const wrap_js_1 = __importDefault(require("./wrap.js")); +function updateIfElse(object, predicate, trueUpdates, falseUpdates) { + const test = typeof predicate === "function" ? predicate(object) : predicate; + const updates = test ? trueUpdates : falseUpdates; + return (0, update_js_1.default)(object, updates); +} +exports.default = (0, wrap_js_1.default)(updateIfElse); +//# sourceMappingURL=ifElse.js.map \ No newline at end of file diff --git a/commonjs/ifElse.js.map b/commonjs/ifElse.js.map new file mode 100644 index 0000000..4a872b9 --- /dev/null +++ b/commonjs/ifElse.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.js","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":";;;;;AAAA,4DAAiC;AACjC,wDAA6B;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY;IAChE,MAAM,IAAI,GAAG,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE7E,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;IAElD,OAAO,IAAA,mBAAM,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AASD,kBAAe,IAAA,iBAAI,EAAC,YAAY,CAAW,CAAC"} \ No newline at end of file diff --git a/commonjs/ifElse.test.d.ts b/commonjs/ifElse.test.d.ts new file mode 100644 index 0000000..d8022ab --- /dev/null +++ b/commonjs/ifElse.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=ifElse.test.d.ts.map \ No newline at end of file diff --git a/commonjs/ifElse.test.d.ts.map b/commonjs/ifElse.test.d.ts.map new file mode 100644 index 0000000..6ed44d0 --- /dev/null +++ b/commonjs/ifElse.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.d.ts","sourceRoot":"","sources":["../src/ifElse.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/ifElse.test.js b/commonjs/ifElse.test.js new file mode 100644 index 0000000..824763e --- /dev/null +++ b/commonjs/ifElse.test.js @@ -0,0 +1,43 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const ifElse_js_1 = __importDefault(require("./ifElse.js")); +const if_js_1 = __importDefault(require("./if.js")); +(0, vitest_1.describe)("ifElse", () => { + (0, vitest_1.test)("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = (0, ifElse_js_1.default)(false, { b: 1 }, { b: 2 })(object); + (0, vitest_1.expect)(result).to.eql({ a: 0, b: 2 }); + }); + (0, vitest_1.test)("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = (0, ifElse_js_1.default)(true, { b: 1 }, { b: 4 })(object); + (0, vitest_1.expect)(result).to.eql({ a: 0, b: 1 }); + }); + (0, vitest_1.test)("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = (0, ifElse_js_1.default)(aIsThree, { b: 1 }, { b: 4 })(object); + (0, vitest_1.expect)(result).to.eql({ a: 0, b: 4 }); + }); + (0, vitest_1.test)("freezes the result", () => { + (0, vitest_1.expect)(Object.isFrozen((0, ifElse_js_1.default)(true, {}, {})({}))).to.be.true; + (0, vitest_1.expect)(Object.isFrozen((0, ifElse_js_1.default)(false, {}, {})({}))).to.be.true; + }); +}); +(0, vitest_1.describe)("if", () => { + (0, vitest_1.test)("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = (0, if_js_1.default)(false, { b: 1 })(object); + (0, vitest_1.expect)(result).not.toHaveProperty("b"); + }); + (0, vitest_1.test)("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = (0, if_js_1.default)(true, { b: 1 })(object); + (0, vitest_1.expect)(result).to.eql({ a: 0, b: 1 }); + }); +}); +//# sourceMappingURL=ifElse.test.js.map \ No newline at end of file diff --git a/commonjs/ifElse.test.js.map b/commonjs/ifElse.test.js.map new file mode 100644 index 0000000..4af4dda --- /dev/null +++ b/commonjs/ifElse.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.js","sourceRoot":"","sources":["../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G,EAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC3C,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/index.d.ts b/commonjs/index.d.ts new file mode 100644 index 0000000..49c130d --- /dev/null +++ b/commonjs/index.d.ts @@ -0,0 +1,3 @@ +declare const u: any; +export default u; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/commonjs/index.d.ts.map b/commonjs/index.d.ts.map new file mode 100644 index 0000000..e29798d --- /dev/null +++ b/commonjs/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,QAAA,MAAM,CAAC,EAAE,GAAY,CAAC;AAWtB,eAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/index.js b/commonjs/index.js new file mode 100644 index 0000000..75b0886 --- /dev/null +++ b/commonjs/index.js @@ -0,0 +1,46 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const freeze_js_1 = __importDefault(require("./freeze.js")); +const is_js_1 = __importDefault(require("./is.js")); +const if_js_1 = __importDefault(require("./if.js")); +const ifElse_js_1 = __importDefault(require("./ifElse.js")); +const update_js_1 = __importStar(require("./update.js")); +const updateIn_js_1 = __importDefault(require("./updateIn.js")); +const constant_js_1 = __importDefault(require("./constant.js")); +const u = update_js_1.default; +u.constant = constant_js_1.default; +u.if = if_js_1.default; +u.ifElse = ifElse_js_1.default; +u.is = is_js_1.default; +u.freeze = freeze_js_1.default; +u.update = update_js_1.default; +u.updateIn = updateIn_js_1.default; +u.omitted = update_js_1.omitted; +exports.default = u; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/commonjs/index.js.map b/commonjs/index.js.map new file mode 100644 index 0000000..fc5b3d3 --- /dev/null +++ b/commonjs/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAAiC;AACjC,oDAAyB;AACzB,oDAA0B;AAC1B,4DAAiC;AACjC,yDAA8C;AAC9C,gEAAqC;AACrC,gEAAqC;AAErC,MAAM,CAAC,GAAQ,mBAAM,CAAC;AAEtB,CAAC,CAAC,QAAQ,GAAG,qBAAQ,CAAC;AACtB,CAAC,CAAC,EAAE,GAAG,eAAG,CAAC;AACX,CAAC,CAAC,MAAM,GAAG,mBAAM,CAAC;AAClB,CAAC,CAAC,EAAE,GAAG,eAAE,CAAC;AACV,CAAC,CAAC,MAAM,GAAG,mBAAM,CAAC;AAClB,CAAC,CAAC,MAAM,GAAG,mBAAM,CAAC;AAClB,CAAC,CAAC,QAAQ,GAAG,qBAAQ,CAAC;AACtB,CAAC,CAAC,OAAO,GAAG,mBAAO,CAAC;AAEpB,kBAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/is.d.ts b/commonjs/is.d.ts new file mode 100644 index 0000000..bb1ddfd --- /dev/null +++ b/commonjs/is.d.ts @@ -0,0 +1,7 @@ +interface Is { + (object: any, path: string | number | (number | string)[], predicate: any): boolean; + (path: string | number | (number | string)[], predicate: any): (object: any) => boolean; +} +declare const _default: Is; +export default _default; +//# sourceMappingURL=is.d.ts.map \ No newline at end of file diff --git a/commonjs/is.d.ts.map b/commonjs/is.d.ts.map new file mode 100644 index 0000000..25c05b7 --- /dev/null +++ b/commonjs/is.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.d.ts","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":"AAsBA,UAAU,EAAE;IACR,CAAC,MAAM,KAAA,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,OAAO,CAAC;IAC1E,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,OAAO,CAAC;CACjF;;AAED,wBAA+B"} \ No newline at end of file diff --git a/commonjs/is.js b/commonjs/is.js new file mode 100644 index 0000000..38aef16 --- /dev/null +++ b/commonjs/is.js @@ -0,0 +1,21 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const splitPath_js_1 = __importDefault(require("./util/splitPath.js")); +const wrap_js_1 = __importDefault(require("./wrap.js")); +function _is(object, path, predicate) { + const parts = (0, splitPath_js_1.default)(path); + for (const part of parts) { + if (typeof object === "undefined") + return false; + object = object[part]; + } + if (typeof predicate === "function") { + return predicate(object); + } + return predicate === object; +} +exports.default = (0, wrap_js_1.default)(_is); +//# sourceMappingURL=is.js.map \ No newline at end of file diff --git a/commonjs/is.js.map b/commonjs/is.js.map new file mode 100644 index 0000000..6587f62 --- /dev/null +++ b/commonjs/is.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.js","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":";;;;;AAAA,uEAA4C;AAC5C,wDAA6B;AAE7B,SAAS,GAAG,CACR,MAAM,EACN,IAA2C,EAC3C,SAAS;IAET,MAAM,KAAK,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,IAAI,OAAO,MAAM,KAAK,WAAW;YAAE,OAAO,KAAK,CAAC;QAChD,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;KACzB;IAED,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;QACjC,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;KAC5B;IAED,OAAO,SAAS,KAAK,MAAM,CAAC;AAChC,CAAC;AAOD,kBAAe,IAAA,iBAAI,EAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/commonjs/is.test.d.ts b/commonjs/is.test.d.ts new file mode 100644 index 0000000..ed225e5 --- /dev/null +++ b/commonjs/is.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=is.test.d.ts.map \ No newline at end of file diff --git a/commonjs/is.test.d.ts.map b/commonjs/is.test.d.ts.map new file mode 100644 index 0000000..1e7cd13 --- /dev/null +++ b/commonjs/is.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.d.ts","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/is.test.js b/commonjs/is.test.js new file mode 100644 index 0000000..99dc03a --- /dev/null +++ b/commonjs/is.test.js @@ -0,0 +1,52 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const is_js_1 = __importDefault(require("./is.js")); +(0, vitest_1.test)("returns true if path matches a value predicate", () => { + const result = (0, is_js_1.default)({ a: { b: 4 } }, "a.b", 4); + (0, vitest_1.expect)(result).to.be.true; +}); +(0, vitest_1.test)("returns true if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = (0, is_js_1.default)("a.b", isEven)({ a: { b: 6 } }); + (0, vitest_1.expect)(result).to.be.true; +}); +(0, vitest_1.test)("returns false if path matches a value predicate", () => { + const result = (0, is_js_1.default)("a.b", 4)({ a: { b: 5 } }); + (0, vitest_1.expect)(result).to.be.false; +}); +(0, vitest_1.test)("returns false if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = (0, is_js_1.default)("a.b", isEven)({ a: { b: 7 } }); + (0, vitest_1.expect)(result).to.be.false; +}); +(0, vitest_1.test)("returns false if the path does not exist", () => { + const result = (0, is_js_1.default)("a.b.c.d", 4)({ a: { b: {} } }); + (0, vitest_1.expect)(result).to.be.false; +}); +(0, vitest_1.test)("can test for undefined", () => { + const result = (0, is_js_1.default)("a.b.c", undefined)({ a: { b: {} } }); + (0, vitest_1.expect)(result).to.be.true; +}); +(0, vitest_1.test)("tests the actual object if a blank path is given", () => { + const result = (0, is_js_1.default)(4, "", 4); + (0, vitest_1.expect)(result).toBeTruthy(); +}); +(0, vitest_1.test)("can use arrays as paths", () => { + const result = (0, is_js_1.default)(["a", "b"], 4)({ a: { b: 4 } }); + (0, vitest_1.expect)(result).to.be.true; +}); +(0, vitest_1.test)("can include array indexes in paths", () => { + let result = (0, is_js_1.default)("a.1.b", 4)({ a: [{}, { b: 4 }] }); + (0, vitest_1.expect)(result).to.be.true; + result = (0, is_js_1.default)(["a", 1, "b"], 4)({ a: [{}, { b: 4 }] }); + (0, vitest_1.expect)(result).to.be.true; +}); +(0, vitest_1.test)("can be partially applied", () => { + const result = (0, is_js_1.default)("a.b", 4)({ a: { b: 4 } }); + (0, vitest_1.expect)(result).to.be.true; +}); +//# sourceMappingURL=is.test.js.map \ No newline at end of file diff --git a/commonjs/is.test.js.map b/commonjs/is.test.js.map new file mode 100644 index 0000000..d7e4cc6 --- /dev/null +++ b/commonjs/is.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.js","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAAsC;AACtC,oDAAyB;AAEzB,IAAA,aAAI,EAAC,gDAAgD,EAAE,GAAG,EAAE;IACxD,MAAM,MAAM,GAAG,IAAA,eAAE,EAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAE7C,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC;AAEH,IAAA,aAAI,EAAC,mDAAmD,EAAE,GAAG,EAAE;IAC3D,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,IAAA,eAAE,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAClD,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC;AAEH,IAAA,aAAI,EAAC,iDAAiD,EAAE,GAAG,EAAE;IACzD,MAAM,MAAM,GAAG,IAAA,eAAE,EAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7C,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC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s/map.d.ts b/commonjs/map.d.ts new file mode 100644 index 0000000..a4e9f5a --- /dev/null +++ b/commonjs/map.d.ts @@ -0,0 +1,7 @@ +interface Map { + (object: any, iteratee: any): any; + (iteratee: any): (object: any) => any; +} +declare const _default: Map; +export default _default; +//# sourceMappingURL=map.d.ts.map \ No newline at end of file diff --git a/commonjs/map.d.ts.map b/commonjs/map.d.ts.map new file mode 100644 index 0000000..f7f85d8 --- /dev/null +++ b/commonjs/map.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAuBA,UAAU,GAAG;IACX,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,GAAG,GAAG,CAAC;IACxB,CAAC,QAAQ,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAC7B;;AAED,wBAAgC"} \ No newline at end of file diff --git a/commonjs/map.js b/commonjs/map.js new file mode 100644 index 0000000..e1998fc --- /dev/null +++ b/commonjs/map.js @@ -0,0 +1,24 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const remeda_1 = require("remeda"); +const update_js_1 = __importDefault(require("./update.js")); +const wrap_js_1 = __importDefault(require("./wrap.js")); +function shallowEqual(object, otherObject) { + for (const k in otherObject) { + if (otherObject[k] !== object[k]) + return false; + } + return true; +} +function map(object, iteratee) { + const updater = typeof iteratee === "function" ? iteratee : (0, update_js_1.default)(iteratee); + const mapper = Array.isArray(object) ? remeda_1.map.indexed : remeda_1.mapValues; + const newObject = mapper(object, updater); + const equal = shallowEqual(object, newObject); + return equal ? object : newObject; +} +exports.default = (0, wrap_js_1.default)(map); +//# sourceMappingURL=map.js.map \ No newline at end of file diff --git a/commonjs/map.js.map b/commonjs/map.js.map new file mode 100644 index 0000000..d643afa --- /dev/null +++ b/commonjs/map.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":";;;;;AAAA,mCAAgD;AAEhD,4DAAiC;AACjC,wDAA6B;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,WAAW;IACvC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE;QAC3B,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;KAChD;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,GAAG,CAAC,MAAM,EAAE,QAAQ;IAC3B,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAA,mBAAM,EAAC,QAAQ,CAAC,CAAC;IAE7E,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,YAAI,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAS,CAAC;IAEhE,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACpC,CAAC;AAOD,kBAAe,IAAA,iBAAI,EAAC,GAAG,CAAQ,CAAC"} \ No newline at end of file diff --git a/commonjs/map.test.d.ts b/commonjs/map.test.d.ts new file mode 100644 index 0000000..3be85f6 --- /dev/null +++ b/commonjs/map.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=map.test.d.ts.map \ No newline at end of file diff --git a/commonjs/map.test.d.ts.map b/commonjs/map.test.d.ts.map new file mode 100644 index 0000000..ef09e8b --- /dev/null +++ b/commonjs/map.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.d.ts","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/map.test.js b/commonjs/map.test.js new file mode 100644 index 0000000..1c17be1 --- /dev/null +++ b/commonjs/map.test.js @@ -0,0 +1,51 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const map_js_1 = __importDefault(require("./map.js")); +(0, vitest_1.describe)("map", () => { + (0, vitest_1.test)("applies updates to each item in an array", () => { + const object = [0, 1, 2]; + const inc = (x) => x + 1; + const result = (0, map_js_1.default)(object, inc); + (0, vitest_1.expect)(result).to.eql([1, 2, 3]); + }); + (0, vitest_1.test)("applies updates to each value in an object", () => { + const object = { a: 0, b: 1, c: 2 }; + const inc = (x) => x + 1; + const result = (0, map_js_1.default)(inc)(object); + (0, vitest_1.expect)(result).to.eql({ a: 1, b: 2, c: 3 }); + }); + (0, vitest_1.test)("can update with a regular updates object", () => { + const object = [{ a: 0 }, { a: 0 }]; + const result = (0, map_js_1.default)({ a: 1 })(object); + (0, vitest_1.expect)(result).to.eql([{ a: 1 }, { a: 1 }]); + }); + (0, vitest_1.test)("returns the same object if no updates are made", () => { + const array = [0, 1]; + const ident = (x) => x; + let result = (0, map_js_1.default)(ident)(array); + (0, vitest_1.expect)(result).to.equal(array); + const object = { a: 0 }; + result = (0, map_js_1.default)(ident)(object); + (0, vitest_1.expect)(result).to.equal(object); + }); + (0, vitest_1.test)("passes the key or index as the second parameter to the iteratee", () => { + const object = { + a: { x: 0 }, + b: [3, 3], + }; + const setToKey = (_, key) => key; + const result = (0, map_js_1.default)(object, (0, map_js_1.default)(setToKey)); + (0, vitest_1.expect)(result).to.eql({ + a: { x: "x" }, + b: [0, 1], + }); + }); + (0, vitest_1.test)("freezes the result", () => { + (0, vitest_1.expect)(Object.isFrozen((0, map_js_1.default)({}, {}))).to.be.true; + }); +}); +//# sourceMappingURL=map.test.js.map \ No newline at end of file diff --git a/commonjs/map.test.js.map b/commonjs/map.test.js.map new file mode 100644 index 0000000..ecbc6cd --- /dev/null +++ b/commonjs/map.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.js","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAAgD;AAChD,sDAA2B;AAE3B,IAAA,iBAAQ,EAAC,KAAK,EAAE,GAAG,EAAE;IACnB,IAAA,aAAI,EAAC,0CAA0C,EAAE,GAAG,EAAE;QACpD,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACzB,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;QACzB,MAAM,MAAM,GAAG,IAAA,gBAAG,EAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAEhC,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,4CAA4C,EAAE,GAAG,EAAE;QACtD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACpC,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;QACzB,MAAM,MAAM,GAAG,IAAA,gBAAG,EAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;QAEhC,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,0CAA0C,EAAE,GAAG,EAAE;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,CAAC;QACjC,MAAM,MAAM,GAAG,IAAA,gBAAG,EAAC,MAAM,EAAE,IAAA,gBAAG,EAAC,QAAQ,CAAC,CAAC,CAAC;QAE1C,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC;YACpB,CAAC,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE;YACb,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;SACV,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,IAAA,aAAI,EAAC,oBAAoB,EAAE,GAAG,EAAE;QAC9B,IAAA,eAAM,EAAC,MAAM,CAAC,QAAQ,CAAC,IAAA,gBAAG,EAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/update.d.ts b/commonjs/update.d.ts new file mode 100644 index 0000000..8c488f3 --- /dev/null +++ b/commonjs/update.d.ts @@ -0,0 +1,10 @@ +export declare const omitted: () => Readonly<{ + __omitted: boolean; +}>; +interface Update { + (object: any, func: any): any; + (func: any): (object: any) => any; +} +declare const _default: Update; +export default _default; +//# sourceMappingURL=update.d.ts.map \ No newline at end of file diff --git a/commonjs/update.d.ts.map b/commonjs/update.d.ts.map new file mode 100644 index 0000000..d6c776f --- /dev/null +++ b/commonjs/update.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO;;EAAyB,CAAC;AAkG9C,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,GAAG,GAAG,CAAC;IACpB,CAAC,IAAI,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CACzB;;AAED,wBAAsC"} \ No newline at end of file diff --git a/commonjs/update.js b/commonjs/update.js new file mode 100644 index 0000000..2667f61 --- /dev/null +++ b/commonjs/update.js @@ -0,0 +1,78 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.omitted = void 0; +const wrap_js_1 = __importDefault(require("./wrap.js")); +const constant_js_1 = __importDefault(require("./constant.js")); +const remeda_1 = require("remeda"); +const innerOmitted = { __omitted: true }; +exports.omitted = (0, constant_js_1.default)(innerOmitted); +function isEmpty(object) { + return !Object.keys(object).length; +} +function reduce(object, callback, initialValue) { + return Object.keys(object).reduce((acc, key) => callback(acc, object[key], key), initialValue); +} +function resolveUpdates(updates, object) { + return reduce(updates, (acc, value, key) => { + let updatedValue = value; + if (!Array.isArray(value) && + value !== null && + typeof value === "object") { + updatedValue = update(object[key], value); // eslint-disable-line no-use-before-define + } + else if (typeof value === "function") { + updatedValue = value(object[key]); + } + if (object[key] !== updatedValue) { + acc[key] = updatedValue; // eslint-disable-line no-param-reassign + } + return acc; + }, {}); +} +function updateArray(updates, object) { + const newArray = [...object]; + Object.keys(updates).forEach((key) => { + newArray[key] = updates[key]; + }); + return newArray; +} +const isPlainObject = (value) => (value === null || value === void 0 ? void 0 : value.constructor) === Object; +/** + * Recursively update an object or array. + * + * Can update with values: + * update({ foo: 3 }, { foo: 1, bar: 2 }); + * // => { foo: 3, bar: 2 } + * + * Or with a function: + * update({ foo: x => (x + 1) }, { foo: 2 }); + * // => { foo: 3 } + * + * @function + * @name update + * @param {Object|Function} updates + * @param {Object|Array} object to update + * @return {Object|Array} new object with modifications + */ +function update(object, updates) { + if (typeof updates === "function") { + return updates(object); + } + if (!isPlainObject(updates)) { + return updates; + } + const defaultedObject = typeof object === "undefined" || object === null ? {} : object; + const resolvedUpdates = resolveUpdates(updates, defaultedObject); + if (isEmpty(resolvedUpdates)) { + return defaultedObject; + } + if (Array.isArray(defaultedObject)) { + return updateArray(resolvedUpdates, defaultedObject).filter((value) => value !== innerOmitted); + } + return (0, remeda_1.omitBy)(Object.assign(Object.assign({}, defaultedObject), resolvedUpdates), (value) => value === innerOmitted); +} +exports.default = (0, wrap_js_1.default)(update); +//# sourceMappingURL=update.js.map \ No newline at end of file diff --git a/commonjs/update.js.map b/commonjs/update.js.map new file mode 100644 index 0000000..b6b210d --- /dev/null +++ b/commonjs/update.js.map @@ -0,0 +1 @@ +{"version":3,"file":"update.js","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA6B;AAC7B,gEAAqC;AAErC,mCAA0C;AAE1C,MAAM,YAAY,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;AAC5B,QAAA,OAAO,GAAG,IAAA,qBAAQ,EAAC,YAAY,CAAC,CAAC;AAE9C,SAAS,OAAO,CAAC,MAAM;IACrB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;AACrC,CAAC;AAED,SAAS,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,YAAY;IAC5C,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAC/B,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAC7C,YAAY,CACb,CAAC;AACJ,CAAC;AAED,SAAS,cAAc,CAAC,OAAO,EAAE,MAAM;IACrC,OAAO,MAAM,CACX,OAAO,EACP,CAAC,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;QAClB,IAAI,YAAY,GAAG,KAAK,CAAC;QAEzB,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACrB,KAAK,KAAK,IAAI;YACd,OAAO,KAAK,KAAK,QAAQ,EACzB;YACA,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,2CAA2C;SACvF;a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CAAC;KACxB;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE;QAClC,OAAO,WAAW,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC,MAAM,CACzD,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,YAAY,CAClC,CAAC;KACH;IAED,OAAO,IAAA,eAAM,kCACN,eAAe,GAAK,eAAe,GACxC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,YAAY,CAClC,CAAC;AACJ,CAAC;AAOD,kBAAe,IAAA,iBAAI,EAAC,MAAM,CAAW,CAAC"} \ No newline at end of file diff --git a/commonjs/updateIn.d.ts b/commonjs/updateIn.d.ts new file mode 100644 index 0000000..f559b67 --- /dev/null +++ b/commonjs/updateIn.d.ts @@ -0,0 +1,7 @@ +interface UpdateIn { + (object: any, path: any, value: any): any; + (path: any, value: any): (object: any) => any; +} +declare const _default: UpdateIn; +export default _default; +//# sourceMappingURL=updateIn.d.ts.map \ No newline at end of file diff --git a/commonjs/updateIn.d.ts.map b/commonjs/updateIn.d.ts.map new file mode 100644 index 0000000..488ee8e --- /dev/null +++ b/commonjs/updateIn.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.d.ts","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":"AA4BA,UAAU,QAAQ;IAChB,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,GAAG,CAAC;IAC3B,CAAC,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAChC;;AAED,wBAA0C"} \ No newline at end of file diff --git a/commonjs/updateIn.js b/commonjs/updateIn.js new file mode 100644 index 0000000..5b95e31 --- /dev/null +++ b/commonjs/updateIn.js @@ -0,0 +1,26 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const update_js_1 = __importDefault(require("./update.js")); +const splitPath_js_1 = __importDefault(require("./util/splitPath.js")); +const wrap_js_1 = __importDefault(require("./wrap.js")); +const map_js_1 = __importDefault(require("./map.js")); +const wildcard = "*"; +function reducePath(acc, key) { + if (key === wildcard) { + return (value) => Object.prototype.hasOwnProperty.call(value, wildcard) + ? // If we actually have wildcard as a property, update that + (0, update_js_1.default)(value, { [wildcard]: acc }) + : // Otherwise map over all properties + (0, map_js_1.default)(value, acc); + } + return { [key]: acc }; +} +function updateIn(object, path, value) { + const updates = (0, splitPath_js_1.default)(path).reduceRight(reducePath, value); + return (0, update_js_1.default)(object, updates); +} +exports.default = (0, wrap_js_1.default)(updateIn); +//# sourceMappingURL=updateIn.js.map \ No newline at end of file diff --git a/commonjs/updateIn.js.map b/commonjs/updateIn.js.map new file mode 100644 index 0000000..5e8fadc --- /dev/null +++ b/commonjs/updateIn.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.js","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":";;;;;AAEA,4DAAiC;AACjC,uEAA4C;AAC5C,wDAA6B;AAC7B,sDAA2B;AAE3B,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB,SAAS,UAAU,CAAC,GAAG,EAAE,GAAG;IAC1B,IAAI,GAAG,KAAK,QAAQ,EAAE;QACpB,OAAO,CAAC,KAAK,EAAE,EAAE,CACf,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC;YACnD,CAAC,CAAC,0DAA0D;gBAC1D,IAAA,mBAAM,EAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC;YACpC,CAAC,CAAC,oCAAoC;gBACpC,IAAA,gBAAG,EAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KACvB;IAED,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK;IACnC,MAAM,OAAO,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAE/D,OAAO,IAAA,mBAAM,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AAOD,kBAAe,IAAA,iBAAI,EAAC,QAAQ,CAAa,CAAC"} \ No newline at end of file diff --git a/commonjs/updateIn.test.d.ts b/commonjs/updateIn.test.d.ts new file mode 100644 index 0000000..6a23d85 --- /dev/null +++ b/commonjs/updateIn.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updateIn.test.d.ts.map \ No newline at end of file diff --git a/commonjs/updateIn.test.d.ts.map b/commonjs/updateIn.test.d.ts.map new file mode 100644 index 0000000..6be947e --- /dev/null +++ b/commonjs/updateIn.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.test.d.ts","sourceRoot":"","sources":["../src/updateIn.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/updateIn.test.js b/commonjs/updateIn.test.js new file mode 100644 index 0000000..0b38733 --- /dev/null +++ b/commonjs/updateIn.test.js @@ -0,0 +1,57 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const updateIn_js_1 = __importDefault(require("./updateIn.js")); +(0, vitest_1.describe)("updateIn", () => { + (0, vitest_1.test)("can update a single path described with a string", () => { + const object = { a: { b: 0 } }; + const result = (0, updateIn_js_1.default)("a.b", 3)(object); + (0, vitest_1.expect)(result).to.eql({ a: { b: 3 } }); + }); + (0, vitest_1.test)("can update a single path described with a string with a function", () => { + const inc = (x) => x + 1; + const object = { a: { b: 0 } }; + const result = (0, updateIn_js_1.default)("a.b", inc)(object); + (0, vitest_1.expect)(result).to.eql({ a: { b: 1 } }); + }); + (0, vitest_1.test)("can update a single path described with an array", () => { + const object = { a: { b: 0 } }; + const result = (0, updateIn_js_1.default)(["a", "b"], 3)(object); + (0, vitest_1.expect)(result).to.eql({ a: { b: 3 } }); + }); + (0, vitest_1.test)("can update arrays", () => { + const object = { a: [0, 0, 0] }; + const result = (0, updateIn_js_1.default)("a.1", 3)(object); + (0, vitest_1.expect)(result).to.eql({ a: [0, 3, 0] }); + }); + (0, vitest_1.test)("can be partially applied", () => { + const object = { a: { b: 0 } }; + const result = (0, updateIn_js_1.default)("a.b", 3)(object); + (0, vitest_1.expect)(result).to.eql({ a: { b: 3 } }); + }); + (0, vitest_1.test)("replaces the object outright if the path is empty", () => { + const object = {}; + const result = (0, updateIn_js_1.default)("", 3)(object); + (0, vitest_1.expect)(result).to.equal(3); + }); + (0, vitest_1.test)("freezes the result", () => { + (0, vitest_1.expect)(Object.isFrozen((0, updateIn_js_1.default)("a", 0)({}))).to.be.true; + }); + (0, vitest_1.test)("can multiple elements of an array with *", () => { + let object = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] }; + let result = (0, updateIn_js_1.default)("a.*.b", (x) => x + 1)(object); + (0, vitest_1.expect)(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] }); + object = { a: [0, 1, 2] }; + result = (0, updateIn_js_1.default)(["a", "*"], (x) => x + 1)(object); + (0, vitest_1.expect)(result).to.eql({ a: [1, 2, 3] }); + }); + (0, vitest_1.test)("can update properties named *", () => { + const object = { "*": 1, x: 1 }; + const result = (0, updateIn_js_1.default)("*", (x) => x + 1)(object); + (0, vitest_1.expect)(result).to.eql({ "*": 2, x: 1 }); + }); +}); +//# sourceMappingURL=updateIn.test.js.map \ No newline at end of file diff --git a/commonjs/updateIn.test.js.map b/commonjs/updateIn.test.js.map new file mode 100644 index 0000000..64bfef6 --- /dev/null +++ b/commonjs/updateIn.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.test.js","sourceRoot":"","sources":["../src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H,IAAA,aAAI,EAAC,+BAA+B,EAAE,GAAG,EAAE;QACzC,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,IAAA,qBAAQ,EAAC,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACnD,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/updeep.bench.d.ts b/commonjs/updeep.bench.d.ts new file mode 100644 index 0000000..87237b2 --- /dev/null +++ b/commonjs/updeep.bench.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.bench.d.ts.map \ No newline at end of file diff --git a/commonjs/updeep.bench.d.ts.map b/commonjs/updeep.bench.d.ts.map new file mode 100644 index 0000000..393d6cf --- /dev/null +++ b/commonjs/updeep.bench.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.d.ts","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/updeep.bench.js b/commonjs/updeep.bench.js new file mode 100644 index 0000000..c182b6f --- /dev/null +++ b/commonjs/updeep.bench.js @@ -0,0 +1,15 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const updeep_1 = __importDefault(require("updeep")); +const index_js_1 = __importDefault(require("./index.js")); +const vitest_1 = require("vitest"); +(0, vitest_1.bench)("original, simple update", () => { + updeep_1.default({ a: 1 })({ a: 2, b: 3 }); +}); +(0, vitest_1.bench)("contender, simple update", () => { + (0, index_js_1.default)({ a: 1 })({ a: 2, b: 3 }); +}); +//# sourceMappingURL=updeep.bench.js.map \ No newline at end of file diff --git a/commonjs/updeep.bench.js.map b/commonjs/updeep.bench.js.map new file mode 100644 index 0000000..af08aea --- /dev/null +++ b/commonjs/updeep.bench.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.js","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":";;;;;AAAA,oDAA8B;AAC9B,0DAAmC;AAEnC,mCAA+B;AAE/B,IAAA,cAAK,EAAC,yBAAyB,EAAE,GAAG,EAAE;IACnC,gBAAgB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,IAAA,cAAK,EAAC,0BAA0B,EAAE,GAAG,EAAE;IACrC,IAAA,kBAAS,EAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/updeep.test.d.ts b/commonjs/updeep.test.d.ts new file mode 100644 index 0000000..c9a3e6b --- /dev/null +++ b/commonjs/updeep.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.test.d.ts.map \ No newline at end of file diff --git a/commonjs/updeep.test.d.ts.map b/commonjs/updeep.test.d.ts.map new file mode 100644 index 0000000..96f1294 --- /dev/null +++ b/commonjs/updeep.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.d.ts","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/updeep.test.js b/commonjs/updeep.test.js new file mode 100644 index 0000000..c7ed2ad --- /dev/null +++ b/commonjs/updeep.test.js @@ -0,0 +1,113 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const index_js_1 = __importDefault(require("./index.js")); +(0, vitest_1.it)("does not change anything if no updates are specified", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = (0, index_js_1.default)(object, {}); + (0, vitest_1.expect)(result).to.equal(object); +}); +(0, vitest_1.it)("can update with fixed values", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = (0, index_js_1.default)(object, { foo: 4 }); + (0, vitest_1.expect)(result).to.deep.equal({ foo: 4, bar: [7, 5] }); +}); +(0, vitest_1.it)("returns the same instance if an update doesn't make changes", () => { + const object = { foo: 3 }; + const result = (0, index_js_1.default)({ foo: 3 })(object); + (0, vitest_1.expect)(result).to.equal(object); +}); +(0, vitest_1.it)("can update a nested structure", () => { + const object = { foo: { bar: 7, bam: 3 }, baz: 32 }; + const result = (0, index_js_1.default)({ foo: { bar: 8 } })(object); + (0, vitest_1.expect)(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); +}); +(0, vitest_1.it)("can update arrays", () => { + const object = [1, 2, 3]; + const result = (0, index_js_1.default)({ 1: 7 })(object); + (0, vitest_1.expect)(result).to.deep.equal([1, 7, 3]); +}); +(0, vitest_1.it)("replaces the object outright if updates are a constant", () => { + (0, vitest_1.expect)((0, index_js_1.default)(3)({})).to.equal(3); + (0, vitest_1.expect)((0, index_js_1.default)(null)({})).to.be.null; +}); +(0, vitest_1.it)("can add an element to an array", () => { + const object = []; + const result = (0, index_js_1.default)({ 0: 3 })(object); + (0, vitest_1.expect)(result).to.eql([3]); +}); +(0, vitest_1.it)("can update nested arrays", () => { + const object = { foo: [1, 2, 3], bar: 9 }; + const result = (0, index_js_1.default)({ foo: { 1: 7 } })(object); + (0, vitest_1.expect)(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); +}); +(0, vitest_1.it)("can use functions to update values", () => { + const inc = (i) => i + 1; + const object = { foo: 3, bar: 4, baz: 7 }; + const result = (0, index_js_1.default)({ foo: inc, bar: inc })(object); + (0, vitest_1.expect)(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); +}); +(0, vitest_1.it)("can be partially applied", () => { + const inc = (i) => i + 1; + const object = { foo: 3 }; + const incFoo = (0, index_js_1.default)({ foo: inc }); + const result = incFoo(object); + (0, vitest_1.expect)(result).to.deep.equal({ foo: 4 }); +}); +(0, vitest_1.it)("can update if the value is an array", () => { + const object = {}; + const result = (0, index_js_1.default)({ foo: [0, 1] })(object); + (0, vitest_1.expect)(result).to.deep.equal({ foo: [0, 1] }); +}); +(0, vitest_1.it)("can update when original object is undefined", () => { + const result = (0, index_js_1.default)({ foo: [0, 1] })(undefined); + (0, vitest_1.expect)(result).to.deep.equal({ foo: [0, 1] }); +}); +(0, vitest_1.it)("can take a function as the updater", () => { + const result = (0, index_js_1.default)((i) => i + 1)(7); + (0, vitest_1.expect)(result).to.eql(8); +}); +(0, vitest_1.it)("deeply freezes the result", () => { + const result = (0, index_js_1.default)({ foo: { bar: 3 } }, { foo: { bar: 0 } }); + (0, vitest_1.expect)(Object.isFrozen(result)).to.be.true; + (0, vitest_1.expect)(Object.isFrozen(result.foo)).to.be.true; +}); +(0, vitest_1.it)("assigns null values", () => { + (0, vitest_1.expect)((0, index_js_1.default)({ isNull: null }, {})).to.eql({ isNull: null }); +}); +(0, vitest_1.it)("defaults to an empty object when null or undefined", () => { + let result = (0, index_js_1.default)({ a: { b: 0 } })({ a: null }); + (0, vitest_1.expect)(result).to.eql({ a: { b: 0 } }); + result = (0, index_js_1.default)({ a: { b: 0 } })({ a: undefined }); + (0, vitest_1.expect)(result).to.eql({ a: { b: 0 } }); + result = (0, index_js_1.default)({ a: { b: 0 } })({}); + (0, vitest_1.expect)(result).to.eql({ a: { b: 0 } }); +}); +(0, vitest_1.it)("preserves empty objects when empty updates are specified", () => { + const result = (0, index_js_1.default)({ a: {} })({}); + (0, vitest_1.expect)(result).to.eql({ a: {} }); +}); +(0, vitest_1.it)("works with date objects", () => { + const date = new Date(); + const result = (0, index_js_1.default)({ created: date })({}); + (0, vitest_1.expect)(result).toEqual({ created: date }); +}); +const expectU = (update, orig, expected) => (0, vitest_1.expect)(update(orig)).to.eql(expected); +(0, vitest_1.describe)("u.omitted", () => { + (0, vitest_1.it)("omit properties via u.omitted", () => { + expectU((0, index_js_1.default)({ a: index_js_1.default.omitted, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }); + }); + (0, vitest_1.it)("omit array and object properties", () => { + expectU((0, index_js_1.default)({ a: index_js_1.default.omitted, b: "stuff", c: index_js_1.default.omitted }), { a: [1, 2, 3], b: "orig", c: { z: "bar" } }, { b: "stuff" }); + }); + (0, vitest_1.it)("deep omit", () => { + expectU((0, index_js_1.default)({ a: { b: index_js_1.default.omitted, c: "stuff" } }), { a: { b: "foo", z: "bar" } }, { a: { z: "bar", c: "stuff" } }); + }); + (0, vitest_1.it)("omitting an array item filters it out", () => { + expectU((0, index_js_1.default)({ 1: index_js_1.default.omitted }), ["a", "b", "c"], ["a", "c"]); + }); +}); +//# sourceMappingURL=updeep.test.js.map \ No newline at end of file diff --git a/commonjs/updeep.test.js.map b/commonjs/updeep.test.js.map new file mode 100644 index 0000000..9f8bfd6 --- /dev/null +++ b/commonjs/updeep.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.js","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAA8C;AAC9C,0DAA2B;AAE3B,IAAA,WAAE,EAAC,sDAAsD,EAAE,GAAG,EAAE;IAC9D,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IACvC,MAAM,MAAM,GAAG,IAAA,kBAAC,EAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAE7B,IAAA,eAAM,EAAC,MAAM,C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,CAAC,IAAA,kBAAC,EAAC,EAAE,CAAC,EAAE,kBAAC,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/util/curry.d.ts b/commonjs/util/curry.d.ts new file mode 100644 index 0000000..d6daf4a --- /dev/null +++ b/commonjs/util/curry.d.ts @@ -0,0 +1,7 @@ +export function curry1(fn: any): (a: any, ...args: any[]) => any; +export function curry2(fn: any): (a: any, b: any, ...args: any[]) => any; +export function curry3(fn: any): (a: any, b: any, c: any, ...args: any[]) => any; +export function curry4(fn: any): (a: any, b: any, c: any, d: any, ...args: any[]) => any; +export default function curry(fn: any, length?: any): any; +export const _: "@@updeep/placeholder"; +//# sourceMappingURL=curry.d.ts.map \ No newline at end of file diff --git a/commonjs/util/curry.d.ts.map b/commonjs/util/curry.d.ts.map new file mode 100644 index 0000000..06c88c6 --- /dev/null +++ b/commonjs/util/curry.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"curry.d.ts","sourceRoot":"","sources":["../../src/util/curry.js"],"names":[],"mappings":"AAcA,iEAQC;AAED,yEAmBC;AAED,iFA6BC;AAED,yFA6CC;AAED,0DAEC;AA5HD,uCAAuC"} \ No newline at end of file diff --git a/commonjs/util/curry.js b/commonjs/util/curry.js new file mode 100644 index 0000000..3dba6ea --- /dev/null +++ b/commonjs/util/curry.js @@ -0,0 +1,123 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.curry4 = exports.curry3 = exports.curry2 = exports.curry1 = exports._ = void 0; +/* eslint no-shadow:0, no-param-reassign:0 prefer-rest-params:0 */ +exports._ = '@@updeep/placeholder'; +function countArguments(args, max) { + let n = args.length; + if (n > max) + n = max; + while (args[n - 1] === exports._) { + n -= 1; + } + return n; +} +function curry1(fn) { + return function curried(a, ...args) { + const [b, c] = args; + const n = countArguments(arguments); + if (n >= 1) + return fn(a, b, c); + return curried; + }; +} +exports.curry1 = curry1; +function curry2(fn) { + return function curried(a, b, ...args) { + const [c, d] = args; + const n = countArguments(arguments, 2); + if (b === exports._ || c === exports._ || d === exports._) { + throw new Error('Can only use placeholder on first argument of this function.'); + } + if (n >= 2) { + if (a === exports._) + return curry1((a, c, d) => fn(a, b, c, d)); + return fn(a, b, c, d); + } + if (n === 1) + return curry1((b, c, d) => fn(a, b, c, d)); + return curried; + }; +} +exports.curry2 = curry2; +function curry3(fn) { + return function curried(a, b, c, ...args) { + const [d, e] = args; + const n = countArguments(arguments, 3); + if (c === exports._ || d === exports._ || e === exports._) { + throw new Error('Can only use placeholder on first or second argument of this function.'); + } + if (n >= 3) { + if (a === exports._) { + if (b === exports._) + return curry2((a, b, d, e) => fn(a, b, c, d, e)); + return curry1((a, d, e) => fn(a, b, c, d, e)); + } + if (b === exports._) + return curry1((b, d, e) => fn(a, b, c, d, e)); + return fn(a, b, c, d, e); + } + if (n === 2) { + if (a === exports._) + return curry2((a, c, d, e) => fn(a, b, c, d, e)); + return curry1((c, d, e) => fn(a, b, c, d, e)); + } + if (n === 1) + return curry2((b, c, d, e) => fn(a, b, c, d, e)); + return curried; + }; +} +exports.curry3 = curry3; +function curry4(fn) { + return function curried(a, b, c, d, ...args) { + const [e, f] = args; + const n = countArguments(arguments, 4); + if (d === exports._ || e === exports._ || f === exports._) { + throw new Error('Can only use placeholder on first, second or third argument of this function.'); + } + if (n >= 4) { + if (a === exports._) { + if (b === exports._) { + if (c === exports._) + return curry3((a, b, c, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === exports._) + return curry2((a, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((a, e, f) => fn(a, b, c, d, e, f)); + } + if (b === exports._) { + if (c === exports._) + return curry2((b, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === exports._) + return curry1((c, e, f) => fn(a, b, c, d, e, f)); + return fn(a, b, c, d, e, f); + } + if (n === 3) { + if (a === exports._) { + if (b === exports._) + return curry3((a, b, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, d, e, f) => fn(a, b, c, d, e, f)); + } + if (b === exports._) + return curry2((b, d, e, f) => fn(a, b, c, d, e, f)); + return curry1((d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 2) { + if (a === exports._) + return curry3((a, c, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((c, d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 1) + return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f)); + return curried; + }; +} +exports.curry4 = curry4; +function curry(fn, length = fn.length) { + return [fn, curry1, curry2, curry3, curry4][length](fn); +} +exports.default = curry; +//# sourceMappingURL=curry.js.map \ No newline at end of file diff --git a/commonjs/util/curry.js.map b/commonjs/util/curry.js.map new file mode 100644 index 0000000..75a80dc --- /dev/null +++ b/commonjs/util/curry.js.map @@ -0,0 +1 @@ +{"version":3,"file":"curry.js","sourceRoot":"","sources":["../../src/util/curry.js"],"names":[],"mappings":";;;AAAA,kEAAkE;AACrD,QAAA,CAAC,GAAG,sBAAsB,CAAA;AAEvC,SAAS,cAAc,CAAC,IAAI,EAAE,GAAG;IAC/B,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,CAAA;IACnB,IAAI,CAAC,GAAG,GAAG;QAAE,CAAC,GAAG,GAAG,CAAA;IAEpB,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,SAAC,EAAE;QACxB,CAAC,IAAI,CAAC,CAAA;KACP;IAED,OAAO,CAAC,CAAA;AACV,CAAC;AAED,SAAgB,MAAM,CAAC,EAAE;IACvB,OAAO,SAAS,OAAO,CAAC,CAAC,EAAE,GAAG,IAAI;QAChC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAA;QACnB,M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CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;SACpD;QAED,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;QACnE,OAAO,OAAO,CAAA;IAChB,CAAC,CAAA;AACH,CAAC;AA7CD,wBA6CC;AAED,SAAwB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,EAAE,CAAC,MAAM;IAClD,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAA;AACzD,CAAC;AAFD,wBAEC"} \ No newline at end of file diff --git a/commonjs/util/splitPath.d.ts b/commonjs/util/splitPath.d.ts new file mode 100644 index 0000000..6daeb5e --- /dev/null +++ b/commonjs/util/splitPath.d.ts @@ -0,0 +1,2 @@ +export default function splitPath(path: string | number | (number | string)[]): (string | number)[]; +//# sourceMappingURL=splitPath.d.ts.map \ No newline at end of file diff --git a/commonjs/util/splitPath.d.ts.map b/commonjs/util/splitPath.d.ts.map new file mode 100644 index 0000000..686b455 --- /dev/null +++ b/commonjs/util/splitPath.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,uBAM5E"} \ No newline at end of file diff --git a/commonjs/util/splitPath.js b/commonjs/util/splitPath.js new file mode 100644 index 0000000..f1c7639 --- /dev/null +++ b/commonjs/util/splitPath.js @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +function splitPath(path) { + if (typeof path === "number") + path = new String(path); + return Array.isArray(path) + ? path + : path.split(".").filter((x) => x !== ""); +} +exports.default = splitPath; +//# sourceMappingURL=splitPath.js.map \ No newline at end of file diff --git a/commonjs/util/splitPath.js.map b/commonjs/util/splitPath.js.map new file mode 100644 index 0000000..07024f1 --- /dev/null +++ b/commonjs/util/splitPath.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.js","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":";;AAAA,SAAwB,SAAS,CAAC,IAA2C;IAC3E,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAQ,CAAC;IAE7D,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACxB,CAAC,CAAC,IAAI;QACN,CAAC,CAAE,IAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;AAC1D,CAAC;AAND,4BAMC"} \ No newline at end of file diff --git a/commonjs/util/splitPath.test.d.ts b/commonjs/util/splitPath.test.d.ts new file mode 100644 index 0000000..daa51d7 --- /dev/null +++ b/commonjs/util/splitPath.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=splitPath.test.d.ts.map \ No newline at end of file diff --git a/commonjs/util/splitPath.test.d.ts.map b/commonjs/util/splitPath.test.d.ts.map new file mode 100644 index 0000000..2866cd6 --- /dev/null +++ b/commonjs/util/splitPath.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/commonjs/util/splitPath.test.js b/commonjs/util/splitPath.test.js new file mode 100644 index 0000000..8478a1d --- /dev/null +++ b/commonjs/util/splitPath.test.js @@ -0,0 +1,23 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const vitest_1 = require("vitest"); +const splitPath_js_1 = __importDefault(require("./splitPath.js")); +(0, vitest_1.it)("treats a number as a single step path", () => { + const path = 1; + const result = (0, splitPath_js_1.default)(path); + (0, vitest_1.expect)(result).to.deep.equal(["1"]); +}); +(0, vitest_1.it)("handles arrays", () => { + const path = ["foo", "bar", "x"]; + const result = (0, splitPath_js_1.default)(path); + (0, vitest_1.expect)(result).to.equal(path); +}); +(0, vitest_1.it)("handles strings separated by dots", () => { + const path = "bar.0.y"; + const result = (0, splitPath_js_1.default)(path); + (0, vitest_1.expect)(result).to.deep.equal(["bar", "0", "y"]); +}); +//# sourceMappingURL=splitPath.test.js.map \ No newline at end of file diff --git a/commonjs/util/splitPath.test.js.map b/commonjs/util/splitPath.test.js.map new file mode 100644 index 0000000..e32404a --- /dev/null +++ b/commonjs/util/splitPath.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.js","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":";;;;;AAAA,mCAAoC;AACpC,kEAAuC;AAEvC,IAAA,WAAE,EAAC,uCAAuC,EAAE,GAAG,EAAE;IAC/C,MAAM,IAAI,GAAG,CAAC,CAAC;IACf,MAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC;IAC/B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC;AAEH,IAAA,WAAE,EAAC,gBAAgB,EAAE,GAAG,EAAE;IACxB,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC;IAC/B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,IAAA,WAAE,EAAC,mCAAmC,EAAE,GAAG,EAAE;IAC3C,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,MAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC;IAC/B,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAClD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/commonjs/wrap.d.ts b/commonjs/wrap.d.ts new file mode 100644 index 0000000..aa294b7 --- /dev/null +++ b/commonjs/wrap.d.ts @@ -0,0 +1,3 @@ +declare const wrap: (fn: any) => (...args: any[]) => Readonly; +export default wrap; +//# sourceMappingURL=wrap.d.ts.map \ No newline at end of file diff --git a/commonjs/wrap.d.ts.map b/commonjs/wrap.d.ts.map new file mode 100644 index 0000000..5061f46 --- /dev/null +++ b/commonjs/wrap.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,IAAI,gDAWP,CAAC;AAEJ,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/commonjs/wrap.js b/commonjs/wrap.js new file mode 100644 index 0000000..2daef0c --- /dev/null +++ b/commonjs/wrap.js @@ -0,0 +1,18 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const freeze_js_1 = __importDefault(require("./freeze.js")); +const wrap = (fn) => (...args) => { + const diff = fn.length - args.length; + if (diff === 0) { + return (0, freeze_js_1.default)(fn(...args)); + } + if (diff === 1) { + return (data) => (0, freeze_js_1.default)(fn(data, ...args)); + } + throw new Error("Wrong number of arguments"); +}; +exports.default = wrap; +//# sourceMappingURL=wrap.js.map \ No newline at end of file diff --git a/commonjs/wrap.js.map b/commonjs/wrap.js.map new file mode 100644 index 0000000..0958336 --- /dev/null +++ b/commonjs/wrap.js.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.js","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":";;;;;AAAA,4DAAiC;AAGjC,MAAM,IAAI,GACR,CAAC,EAAE,EAAE,EAAE,CACP,CAAC,GAAG,IAAI,EAAE,EAAE;IACV,MAAM,IAAI,GAAG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,IAAA,mBAAM,EAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5B;IACD,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,mBAAM,EAAC,EAAE,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5C;IACD,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;AAC/C,CAAC,CAAC;AAEJ,kBAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/dist/constant.d.ts b/dist/constant.d.ts new file mode 100644 index 0000000..0e17823 --- /dev/null +++ b/dist/constant.d.ts @@ -0,0 +1,34 @@ +/** + * Returns a function that always returns the supplied value. + * + * Useful for replacing an object outright rather than merging it. + * + * @function + * @sig a -> (* -> a) + * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. + * + * @example + * var alwaysFour = u.constant(4); + * expect(alwaysFour(32)).toEqual(4); + * + * @example + * var user = { + * name: 'Mitch', + * favorites: { + * band: 'Nirvana', + * movie: 'The Matrix' + * } + * }; + * + * var newFavorites = { + * band: 'Coldplay' + * }; + * + * var result = u({ favorites: u.constant(newFavorites) }, user); + * + * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); + */ +export default function constant(value: T): () => Readonly; +//# sourceMappingURL=constant.d.ts.map \ No newline at end of file diff --git a/dist/constant.d.ts.map b/dist/constant.d.ts.map new file mode 100644 index 0000000..5a1fb8b --- /dev/null +++ b/dist/constant.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,qBAG3C"} \ No newline at end of file diff --git a/dist/constant.js b/dist/constant.js new file mode 100644 index 0000000..45fb1d3 --- /dev/null +++ b/dist/constant.js @@ -0,0 +1,38 @@ +import freeze from "./freeze.js"; +/** + * Returns a function that always returns the supplied value. + * + * Useful for replacing an object outright rather than merging it. + * + * @function + * @sig a -> (* -> a) + * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. + * + * @example + * var alwaysFour = u.constant(4); + * expect(alwaysFour(32)).toEqual(4); + * + * @example + * var user = { + * name: 'Mitch', + * favorites: { + * band: 'Nirvana', + * movie: 'The Matrix' + * } + * }; + * + * var newFavorites = { + * band: 'Coldplay' + * }; + * + * var result = u({ favorites: u.constant(newFavorites) }, user); + * + * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); + */ +export default function constant(value) { + const frozen = freeze(value); + return () => frozen; +} +//# sourceMappingURL=constant.js.map \ No newline at end of file diff --git a/dist/constant.js.map b/dist/constant.js.map new file mode 100644 index 0000000..0fe2057 --- /dev/null +++ b/dist/constant.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.js","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAI,KAAQ;IAC1C,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC"} \ No newline at end of file diff --git a/dist/constant.test.d.ts b/dist/constant.test.d.ts new file mode 100644 index 0000000..7a91645 --- /dev/null +++ b/dist/constant.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=constant.test.d.ts.map \ No newline at end of file diff --git a/dist/constant.test.d.ts.map b/dist/constant.test.d.ts.map new file mode 100644 index 0000000..9efc588 --- /dev/null +++ b/dist/constant.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.d.ts","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/constant.test.js b/dist/constant.test.js new file mode 100644 index 0000000..556615e --- /dev/null +++ b/dist/constant.test.js @@ -0,0 +1,13 @@ +import { expect, test } from "vitest"; +import constant from "./constant.js"; +test("returns what it is given... constantly", () => { + const func = constant(4); + expect(func()).to.equal(4); + expect(func("hi")).to.equal(4); + expect(func("hi", 8)).to.equal(4); + expect(func(4)).to.equal(4); +}); +test("freezes the result", () => { + expect(Object.isFrozen(constant({})())).toBeTruthy(); +}); +//# sourceMappingURL=constant.test.js.map \ No newline at end of file diff --git a/dist/constant.test.js.map b/dist/constant.test.js.map new file mode 100644 index 0000000..ea57055 --- /dev/null +++ b/dist/constant.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.js","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,IAAI,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE;IAChD,MAAM,IAAI,GAAQ,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE9B,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC/B,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;IAC5B,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AACzD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/freeze.d.ts b/dist/freeze.d.ts new file mode 100644 index 0000000..7ae399c --- /dev/null +++ b/dist/freeze.d.ts @@ -0,0 +1,19 @@ +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +declare function freeze(object: T): Readonly; +export default freeze; +//# sourceMappingURL=freeze.d.ts.map \ No newline at end of file diff --git a/dist/freeze.d.ts.map b/dist/freeze.d.ts.map new file mode 100644 index 0000000..6057a76 --- /dev/null +++ b/dist/freeze.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.d.ts","sourceRoot":"","sources":["../src/freeze.ts"],"names":[],"mappings":"AAyBA;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAkBzC;AAED,eAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/dist/freeze.js b/dist/freeze.js new file mode 100644 index 0000000..50a9726 --- /dev/null +++ b/dist/freeze.js @@ -0,0 +1,55 @@ +function isFreezable(object) { + if (object === null) + return false; + if (object instanceof RegExp) + return false; + if (object instanceof ArrayBuffer) + return false; + return Array.isArray(object) || typeof object === "object"; +} +function needsFreezing(object) { + return isFreezable(object) && !Object.isFrozen(object); +} +function recur(object) { + Object.freeze(object); + Object.keys(object).forEach((key) => { + const value = object[key]; + if (needsFreezing(value)) { + recur(value); + } + }); + return object; +} +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +function freeze(object) { + if (typeof process === "undefined") { + return object; + } + if (process.env.NODE_ENV === "production") { + return object; + } + if (process.env.UPDEEP_MODE === "dangerously_never_freeze") { + return object; + } + if (needsFreezing(object)) { + recur(object); + } + return object; +} +export default freeze; +//# sourceMappingURL=freeze.js.map \ No newline at end of file diff --git a/dist/freeze.js.map b/dist/freeze.js.map new file mode 100644 index 0000000..c1cb063 --- /dev/null +++ b/dist/freeze.js.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.js","sourceRoot":"","sources":["../src/freeze.ts"],"names":[],"mappings":"AAAA,SAAS,WAAW,CAAC,MAAe;IAClC,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAClC,IAAI,MAAM,YAAY,MAAM;QAAE,OAAO,KAAK,CAAC;IAC3C,IAAI,MAAM,YAAY,WAAW;QAAE,OAAO,KAAK,CAAC;IAEhD,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,MAAM,KAAK,QAAQ,CAAC;AAC7D,CAAC;AAED,SAAS,aAAa,CAAC,MAAe;IACpC,OAAO,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACzD,CAAC;AAED,SAAS,KAAK,CAAC,MAAM;IACnB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAEtB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAClC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE;YACxB,KAAK,CAAC,KAAK,CAAC,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAS,MAAM,CAAI,MAAS;IAC1B,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QAClC,OAAO,MAAM,CAAC;KACf;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,OAAO,MAAM,CAAC;KACf;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,KAAK,0BAA0B,EAAE;QAC1D,OAAO,MAAM,CAAC;KACf;IAED,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE;QACzB,KAAK,CAAC,MAAM,CAAC,CAAC;KACf;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,eAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/dist/freeze.test.d.ts b/dist/freeze.test.d.ts new file mode 100644 index 0000000..ecc5984 --- /dev/null +++ b/dist/freeze.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=freeze.test.d.ts.map \ No newline at end of file diff --git a/dist/freeze.test.d.ts.map b/dist/freeze.test.d.ts.map new file mode 100644 index 0000000..07a38c1 --- /dev/null +++ b/dist/freeze.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.test.d.ts","sourceRoot":"","sources":["../src/freeze.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/freeze.test.js b/dist/freeze.test.js new file mode 100644 index 0000000..2ba34fa --- /dev/null +++ b/dist/freeze.test.js @@ -0,0 +1,56 @@ +import { describe, afterEach, test, expect } from "vitest"; +import freeze from "./freeze.js"; +describe("freeze", () => { + afterEach(() => { + delete process.env.NODE_ENV; + }); + test("freezes objects", () => { + const object = {}; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + }); + test("freezes nested objects", () => { + const object = { foo: { bar: 3 } }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.true; + }); + test("freezes nested arrays", () => { + const object = [[0]]; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + expect(Object.isFrozen(object[0])).to.be.true; + }); + test("ignores functions", () => { + const object = { foo: () => 1 }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("ignores regexps", () => { + const object = { foo: /\d/ }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("does not freeze children if the parent is already frozen", () => { + const object = { foo: {} }; + Object.freeze(object); + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("does not freeze in production", () => { + process.env.NODE_ENV = "production"; + const object = {}; + freeze(object); + expect(Object.isFrozen(object)).to.be.false; + }); + test("handles null objects", () => { + const object = { foo: null }; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + }); + test("returns the same object", () => { + const object = {}; + const result = freeze(object); + expect(result).to.equal(object); + }); +}); +//# sourceMappingURL=freeze.test.js.map \ No newline at end of file diff --git a/dist/freeze.test.js.map b/dist/freeze.test.js.map new file mode 100644 index 0000000..77a0d3e --- /dev/null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C,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAChC,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAC7C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACnC,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/if.d.ts b/dist/if.d.ts new file mode 100644 index 0000000..f11eafc --- /dev/null +++ b/dist/if.d.ts @@ -0,0 +1,7 @@ +interface If { + (object: any, predicate: any, trueUpdates: any): unknown; + (predicate: any, trueUpdates: any): (unknown: any) => unknown; +} +declare const _default: If; +export default _default; +//# sourceMappingURL=if.d.ts.map \ No newline at end of file diff --git a/dist/if.d.ts.map b/dist/if.d.ts.map new file mode 100644 index 0000000..af7f764 --- /dev/null +++ b/dist/if.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.d.ts","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":"AAKA,UAAU,EAAE;IACV,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,OAAO,CAAC;IAC1C,CAAC,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CAChD;;AAMD,wBAA+B"} \ No newline at end of file diff --git a/dist/if.js b/dist/if.js new file mode 100644 index 0000000..709f226 --- /dev/null +++ b/dist/if.js @@ -0,0 +1,8 @@ +import { identity } from "remeda"; +import ifElse from "./ifElse.js"; +import wrap from "./wrap.js"; +function _if(object, predicate, trueUpdates) { + return ifElse(object, predicate, trueUpdates, identity); +} +export default wrap(_if); +//# sourceMappingURL=if.js.map \ No newline at end of file diff --git a/dist/if.js.map b/dist/if.js.map new file mode 100644 index 0000000..0491bde --- /dev/null +++ b/dist/if.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.js","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAO7B,SAAS,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW;IACzC,OAAO,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAC;AAC1D,CAAC;AAED,eAAe,IAAI,CAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/dist/if.test.d.ts b/dist/if.test.d.ts new file mode 100644 index 0000000..d034164 --- /dev/null +++ b/dist/if.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=if.test.d.ts.map \ No newline at end of file diff --git a/dist/if.test.d.ts.map b/dist/if.test.d.ts.map new file mode 100644 index 0000000..1d2dd89 --- /dev/null +++ b/dist/if.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.d.ts","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/if.test.js b/dist/if.test.js new file mode 100644 index 0000000..588d675 --- /dev/null +++ b/dist/if.test.js @@ -0,0 +1,27 @@ +import { describe, test, expect } from "vitest"; +import _if from "./if.js"; +describe("if", () => { + test("does not update if the predicate is false", () => { + const object = { a: 0 }; + let result = _if(false, { b: 1 }, object); + expect(result).to.eql(object); + result = _if(0, false, 1); + expect(result).to.eql(0); + }); + test("does update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = _if(aIsThree, { b: 1 }, object); + expect(result).to.eql({ a: 0 }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(_if(true, {})({}))).to.be.true; + expect(Object.isFrozen(_if(false, {})({}))).to.be.true; + }); +}); +//# sourceMappingURL=if.test.js.map \ No newline at end of file diff --git a/dist/if.test.js.map b/dist/if.test.js.map new file mode 100644 index 0000000..3b765a8 --- /dev/null +++ b/dist/if.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.js","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,GAAG,MAAM,SAAS,CAAC;AAE1B,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE;IAClB,IAAI,CAAC,2CAA2C,EAAE,GAAG,EAAE;QACrD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,IAAI,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QAC1C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAE9B,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QAC1B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAChD,MAAM,MAAM,G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0 +1,8 @@ +type Predicate = ((source: any) => boolean) | boolean; +interface IfElse { + (object: any, predicate: Predicate, trueUpdates: any, falseUpdates: any): unknown; + (predicate: Predicate, trueUpdates: any, falseUpdates: any): (unknown: any) => unknown; +} +declare const _default: IfElse; +export default _default; +//# sourceMappingURL=ifElse.d.ts.map \ No newline at end of file diff --git a/dist/ifElse.d.ts.map b/dist/ifElse.d.ts.map new file mode 100644 index 0000000..c085b20 --- /dev/null +++ b/dist/ifElse.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.d.ts","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":"AAWA,KAAK,SAAS,GAAG,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,OAAO,CAAC;AAEtD,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,OAAO,CAAC;IACnE,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CACzE;;AAED,wBAA4C"} \ No newline at end of file diff --git a/dist/ifElse.js b/dist/ifElse.js new file mode 100644 index 0000000..f353854 --- /dev/null +++ b/dist/ifElse.js @@ -0,0 +1,9 @@ +import update from "./update.js"; +import wrap from "./wrap.js"; +function updateIfElse(object, predicate, trueUpdates, falseUpdates) { + const test = typeof predicate === "function" ? predicate(object) : predicate; + const updates = test ? trueUpdates : falseUpdates; + return update(object, updates); +} +export default wrap(updateIfElse); +//# sourceMappingURL=ifElse.js.map \ No newline at end of file diff --git a/dist/ifElse.js.map b/dist/ifElse.js.map new file mode 100644 index 0000000..8287535 --- /dev/null +++ b/dist/ifElse.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.js","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY;IAChE,MAAM,IAAI,GAAG,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE7E,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;IAElD,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AASD,eAAe,IAAI,CAAC,YAAY,CAAW,CAAC"} \ No newline at end of file diff --git a/dist/ifElse.test.d.ts b/dist/ifElse.test.d.ts new file mode 100644 index 0000000..d8022ab --- /dev/null +++ b/dist/ifElse.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=ifElse.test.d.ts.map \ No newline at end of file diff --git a/dist/ifElse.test.d.ts.map b/dist/ifElse.test.d.ts.map new file mode 100644 index 0000000..6ed44d0 --- /dev/null +++ b/dist/ifElse.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.d.ts","sourceRoot":"","sources":["../src/ifElse.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/ifElse.test.js b/dist/ifElse.test.js new file mode 100644 index 0000000..4902f57 --- /dev/null +++ b/dist/ifElse.test.js @@ -0,0 +1,38 @@ +import { expect, test, describe } from "vitest"; +import ifElse from "./ifElse.js"; +import _if from "./if.js"; +describe("ifElse", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = ifElse(false, { b: 1 }, { b: 2 })(object); + expect(result).to.eql({ a: 0, b: 2 }); + }); + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = ifElse(true, { b: 1 }, { b: 4 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = ifElse(aIsThree, { b: 1 }, { b: 4 })(object); + expect(result).to.eql({ a: 0, b: 4 }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(ifElse(true, {}, {})({}))).to.be.true; + expect(Object.isFrozen(ifElse(false, {}, {})({}))).to.be.true; + }); +}); +describe("if", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = _if(false, { b: 1 })(object); + expect(result).not.toHaveProperty("b"); + }); + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); +}); +//# sourceMappingURL=ifElse.test.js.map \ No newline at end of file diff --git a/dist/ifElse.test.js.map b/dist/ifElse.test.js.map new file mode 100644 index 0000000..de91212 --- /dev/null +++ b/dist/ifElse.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.js","sourceRoot":"","sources":["../src/ifElse.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,GAAG,MAAM,SAAS,CAAC;AAE1B,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAI,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAChE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uDAAuD,E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,CAAC;IAChE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE;IAClB,IAAI,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAChE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC5C,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uDAAuD,EAAE,GAAG,EAAE;QACjE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC3C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..49c130d --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,3 @@ +declare const u: any; +export default u; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map new file mode 100644 index 0000000..e29798d --- /dev/null +++ b/dist/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,QAAA,MAAM,CAAC,EAAE,GAAY,CAAC;AAWtB,eAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..0ba381d --- /dev/null +++ b/dist/index.js @@ -0,0 +1,18 @@ +import freeze from "./freeze.js"; +import is from "./is.js"; +import _if from "./if.js"; +import ifElse from "./ifElse.js"; +import update, { omitted } from "./update.js"; +import updateIn from "./updateIn.js"; +import constant from "./constant.js"; +const u = update; +u.constant = constant; +u.if = _if; +u.ifElse = ifElse; +u.is = is; +u.freeze = freeze; +u.update = update; +u.updateIn = updateIn; +u.omitted = omitted; +export default u; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..5cae3d4 --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,GAAG,MAAM,SAAS,CAAC;AAC1B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,CAAC,GAAQ,MAAM,CAAC;AAEtB,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACtB,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC;AACX,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;AACV,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACtB,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC;AAEpB,eAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/is.d.ts b/dist/is.d.ts new file mode 100644 index 0000000..bb1ddfd --- /dev/null +++ b/dist/is.d.ts @@ -0,0 +1,7 @@ +interface Is { + (object: any, path: string | number | (number | string)[], predicate: any): boolean; + (path: string | number | (number | string)[], predicate: any): (object: any) => boolean; +} +declare const _default: Is; +export default _default; +//# sourceMappingURL=is.d.ts.map \ No newline at end of file diff --git a/dist/is.d.ts.map b/dist/is.d.ts.map new file mode 100644 index 0000000..25c05b7 --- /dev/null +++ b/dist/is.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.d.ts","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":"AAsBA,UAAU,EAAE;IACR,CAAC,MAAM,KAAA,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,OAAO,CAAC;IAC1E,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,OAAO,CAAC;CACjF;;AAED,wBAA+B"} \ No newline at end of file diff --git a/dist/is.js b/dist/is.js new file mode 100644 index 0000000..ea27999 --- /dev/null +++ b/dist/is.js @@ -0,0 +1,16 @@ +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; +function _is(object, path, predicate) { + const parts = splitPath(path); + for (const part of parts) { + if (typeof object === "undefined") + return false; + object = object[part]; + } + if (typeof predicate === "function") { + return predicate(object); + } + return predicate === object; +} +export default wrap(_is); +//# sourceMappingURL=is.js.map \ No newline at end of file diff --git a/dist/is.js.map b/dist/is.js.map new file mode 100644 index 0000000..fc045d3 --- /dev/null +++ b/dist/is.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.js","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,GAAG,CACR,MAAM,EACN,IAA2C,EAC3C,SAAS;IAET,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,IAAI,OAAO,MAAM,KAAK,WAAW;YAAE,OAAO,KAAK,CAAC;QAChD,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;KACzB;IAED,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;QACjC,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;KAC5B;IAED,OAAO,SAAS,KAAK,MAAM,CAAC;AAChC,CAAC;AAOD,eAAe,IAAI,CAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/dist/is.test.d.ts b/dist/is.test.d.ts new file mode 100644 index 0000000..ed225e5 --- /dev/null +++ b/dist/is.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=is.test.d.ts.map \ No newline at end of file diff --git a/dist/is.test.d.ts.map b/dist/is.test.d.ts.map new file mode 100644 index 0000000..1e7cd13 --- /dev/null +++ b/dist/is.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.d.ts","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/is.test.js b/dist/is.test.js new file mode 100644 index 0000000..e3ea792 --- /dev/null +++ b/dist/is.test.js @@ -0,0 +1,47 @@ +import { expect, test } from "vitest"; +import is from "./is.js"; +test("returns true if path matches a value predicate", () => { + const result = is({ a: { b: 4 } }, "a.b", 4); + expect(result).to.be.true; +}); +test("returns true if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 6 } }); + expect(result).to.be.true; +}); +test("returns false if path matches a value predicate", () => { + const result = is("a.b", 4)({ a: { b: 5 } }); + expect(result).to.be.false; +}); +test("returns false if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 7 } }); + expect(result).to.be.false; +}); +test("returns false if the path does not exist", () => { + const result = is("a.b.c.d", 4)({ a: { b: {} } }); + expect(result).to.be.false; +}); +test("can test for undefined", () => { + const result = is("a.b.c", undefined)({ a: { b: {} } }); + expect(result).to.be.true; +}); +test("tests the actual object if a blank path is given", () => { + const result = is(4, "", 4); + expect(result).toBeTruthy(); +}); +test("can use arrays as paths", () => { + const result = is(["a", "b"], 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); +test("can include array indexes in paths", () => { + let result = is("a.1.b", 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; + result = is(["a", 1, "b"], 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; +}); +test("can be partially applied", () => { + const result = is("a.b", 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); +//# sourceMappingURL=is.test.js.map \ No newline at end of file diff --git a/dist/is.test.js.map b/dist/is.test.js.map new file mode 100644 index 0000000..5eb5008 --- /dev/null +++ b/dist/is.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.js","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,IAAI,CAAC,gDAAgD,EAAE,GAAG,EAAE;IACxD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAE7C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mDAAmD,EAAE,GAAG,EAAE;IAC3D,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC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ClC,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/map.d.ts b/dist/map.d.ts new file mode 100644 index 0000000..a4e9f5a --- /dev/null +++ b/dist/map.d.ts @@ -0,0 +1,7 @@ +interface Map { + (object: any, iteratee: any): any; + (iteratee: any): (object: any) => any; +} +declare const _default: Map; +export default _default; +//# sourceMappingURL=map.d.ts.map \ No newline at end of file diff --git a/dist/map.d.ts.map b/dist/map.d.ts.map new file mode 100644 index 0000000..f7f85d8 --- /dev/null +++ b/dist/map.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAuBA,UAAU,GAAG;IACX,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,GAAG,GAAG,CAAC;IACxB,CAAC,QAAQ,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAC7B;;AAED,wBAAgC"} \ No newline at end of file diff --git a/dist/map.js b/dist/map.js new file mode 100644 index 0000000..b12e7b9 --- /dev/null +++ b/dist/map.js @@ -0,0 +1,19 @@ +import { mapValues, map as _map } from "remeda"; +import update from "./update.js"; +import wrap from "./wrap.js"; +function shallowEqual(object, otherObject) { + for (const k in otherObject) { + if (otherObject[k] !== object[k]) + return false; + } + return true; +} +function map(object, iteratee) { + const updater = typeof iteratee === "function" ? iteratee : update(iteratee); + const mapper = Array.isArray(object) ? _map.indexed : mapValues; + const newObject = mapper(object, updater); + const equal = shallowEqual(object, newObject); + return equal ? object : newObject; +} +export default wrap(map); +//# sourceMappingURL=map.js.map \ No newline at end of file diff --git a/dist/map.js.map b/dist/map.js.map new file mode 100644 index 0000000..a7530fc --- /dev/null +++ b/dist/map.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,GAAG,IAAI,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,WAAW;IACvC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE;QAC3B,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;KAChD;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,GAAG,CAAC,MAAM,EAAE,QAAQ;IAC3B,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAE7E,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAEhE,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACpC,CAAC;AAOD,eAAe,IAAI,CAAC,GAAG,CAAQ,CAAC"} \ No newline at end of file diff --git a/dist/map.test.d.ts b/dist/map.test.d.ts new file mode 100644 index 0000000..3be85f6 --- /dev/null +++ b/dist/map.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=map.test.d.ts.map \ No newline at end of file diff --git a/dist/map.test.d.ts.map b/dist/map.test.d.ts.map new file mode 100644 index 0000000..ef09e8b --- /dev/null +++ b/dist/map.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.d.ts","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/map.test.js b/dist/map.test.js new file mode 100644 index 0000000..c4813ab --- /dev/null +++ b/dist/map.test.js @@ -0,0 +1,46 @@ +import { expect, test, describe } from "vitest"; +import map from "./map.js"; +describe("map", () => { + test("applies updates to each item in an array", () => { + const object = [0, 1, 2]; + const inc = (x) => x + 1; + const result = map(object, inc); + expect(result).to.eql([1, 2, 3]); + }); + test("applies updates to each value in an object", () => { + const object = { a: 0, b: 1, c: 2 }; + const inc = (x) => x + 1; + const result = map(inc)(object); + expect(result).to.eql({ a: 1, b: 2, c: 3 }); + }); + test("can update with a regular updates object", () => { + const object = [{ a: 0 }, { a: 0 }]; + const result = map({ a: 1 })(object); + expect(result).to.eql([{ a: 1 }, { a: 1 }]); + }); + test("returns the same object if no updates are made", () => { + const array = [0, 1]; + const ident = (x) => x; + let result = map(ident)(array); + expect(result).to.equal(array); + const object = { a: 0 }; + result = map(ident)(object); + expect(result).to.equal(object); + }); + test("passes the key or index as the second parameter to the iteratee", () => { + const object = { + a: { x: 0 }, + b: [3, 3], + }; + const setToKey = (_, key) => key; + const result = map(object, map(setToKey)); + expect(result).to.eql({ + a: { x: "x" }, + b: [0, 1], + }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(map({}, {}))).to.be.true; + }); +}); +//# sourceMappingURL=map.test.js.map \ No newline at end of file diff --git a/dist/map.test.js.map b/dist/map.test.js.map new file mode 100644 index 0000000..54bc0e7 --- /dev/null +++ b/dist/map.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.js","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,GAAG,MAAM,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AAC,oBAAoB,EAAE,GAAG,EAAE;QAC9B,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/update.d.ts b/dist/update.d.ts new file mode 100644 index 0000000..8c488f3 --- /dev/null +++ b/dist/update.d.ts @@ -0,0 +1,10 @@ +export declare const omitted: () => Readonly<{ + __omitted: boolean; +}>; +interface Update { + (object: any, func: any): any; + (func: any): (object: any) => any; +} +declare const _default: Update; +export default _default; +//# sourceMappingURL=update.d.ts.map \ No newline at end of file diff --git a/dist/update.d.ts.map b/dist/update.d.ts.map new file mode 100644 index 0000000..d6c776f --- /dev/null +++ b/dist/update.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO;;EAAyB,CAAC;AAkG9C,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,GAAG,GAAG,CAAC;IACpB,CAAC,IAAI,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CACzB;;AAED,wBAAsC"} \ No newline at end of file diff --git a/dist/update.js b/dist/update.js new file mode 100644 index 0000000..e3dfaa7 --- /dev/null +++ b/dist/update.js @@ -0,0 +1,72 @@ +import wrap from "./wrap.js"; +import constant from "./constant.js"; +import { omitBy } from "remeda"; +const innerOmitted = { __omitted: true }; +export const omitted = constant(innerOmitted); +function isEmpty(object) { + return !Object.keys(object).length; +} +function reduce(object, callback, initialValue) { + return Object.keys(object).reduce((acc, key) => callback(acc, object[key], key), initialValue); +} +function resolveUpdates(updates, object) { + return reduce(updates, (acc, value, key) => { + let updatedValue = value; + if (!Array.isArray(value) && + value !== null && + typeof value === "object") { + updatedValue = update(object[key], value); // eslint-disable-line no-use-before-define + } + else if (typeof value === "function") { + updatedValue = value(object[key]); + } + if (object[key] !== updatedValue) { + acc[key] = updatedValue; // eslint-disable-line no-param-reassign + } + return acc; + }, {}); +} +function updateArray(updates, object) { + const newArray = [...object]; + Object.keys(updates).forEach((key) => { + newArray[key] = updates[key]; + }); + return newArray; +} +const isPlainObject = (value) => (value === null || value === void 0 ? void 0 : value.constructor) === Object; +/** + * Recursively update an object or array. + * + * Can update with values: + * update({ foo: 3 }, { foo: 1, bar: 2 }); + * // => { foo: 3, bar: 2 } + * + * Or with a function: + * update({ foo: x => (x + 1) }, { foo: 2 }); + * // => { foo: 3 } + * + * @function + * @name update + * @param {Object|Function} updates + * @param {Object|Array} object to update + * @return {Object|Array} new object with modifications + */ +function update(object, updates) { + if (typeof updates === "function") { + return updates(object); + } + if (!isPlainObject(updates)) { + return updates; + } + const defaultedObject = typeof object === "undefined" || object === null ? {} : object; + const resolvedUpdates = resolveUpdates(updates, defaultedObject); + if (isEmpty(resolvedUpdates)) { + return defaultedObject; + } + if (Array.isArray(defaultedObject)) { + return updateArray(resolvedUpdates, defaultedObject).filter((value) => value !== innerOmitted); + } + return omitBy(Object.assign(Object.assign({}, defaultedObject), resolvedUpdates), (value) => value === innerOmitted); +} +export default wrap(update); +//# sourceMappingURL=update.js.map \ No newline at end of file diff --git a/dist/update.js.map b/dist/update.js.map new file mode 100644 index 0000000..3bab5f6 --- /dev/null +++ b/dist/update.js.map @@ -0,0 +1 @@ +{"version":3,"file":"update.js","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,OAAO,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAE1C,MAAM,YAAY,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;AACzC,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;AA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+interface UpdateIn { + (object: any, path: any, value: any): any; + (path: any, value: any): (object: any) => any; +} +declare const _default: UpdateIn; +export default _default; +//# sourceMappingURL=updateIn.d.ts.map \ No newline at end of file diff --git a/dist/updateIn.d.ts.map b/dist/updateIn.d.ts.map new file mode 100644 index 0000000..488ee8e --- /dev/null +++ b/dist/updateIn.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.d.ts","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":"AA4BA,UAAU,QAAQ;IAChB,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,GAAG,CAAC;IAC3B,CAAC,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAChC;;AAED,wBAA0C"} \ No newline at end of file diff --git a/dist/updateIn.js b/dist/updateIn.js new file mode 100644 index 0000000..4e98868 --- /dev/null +++ b/dist/updateIn.js @@ -0,0 +1,21 @@ +import update from "./update.js"; +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; +import map from "./map.js"; +const wildcard = "*"; +function reducePath(acc, key) { + if (key === wildcard) { + return (value) => Object.prototype.hasOwnProperty.call(value, wildcard) + ? // If we actually have wildcard as a property, update that + update(value, { [wildcard]: acc }) + : // Otherwise map over all properties + map(value, acc); + } + return { [key]: acc }; +} +function updateIn(object, path, value) { + const updates = splitPath(path).reduceRight(reducePath, value); + return update(object, updates); +} +export default wrap(updateIn); +//# sourceMappingURL=updateIn.js.map \ No newline at end of file diff --git a/dist/updateIn.js.map b/dist/updateIn.js.map new file mode 100644 index 0000000..21e4cc2 --- /dev/null +++ b/dist/updateIn.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.js","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,GAAG,MAAM,UAAU,CAAC;AAE3B,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB,SAAS,UAAU,CAAC,GAAG,EAAE,GAAG;IAC1B,IAAI,GAAG,KAAK,QAAQ,EAAE;QACpB,OAAO,CAAC,KAAK,EAAE,EAAE,CACf,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC;YACnD,CAAC,CAAC,0DAA0D;gBAC1D,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC;YACpC,CAAC,CAAC,oCAAoC;gBACpC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KACvB;IAED,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK;IACnC,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAE/D,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AAOD,eAAe,IAAI,CAAC,QAAQ,CAAa,CAAC"} \ No newline at end of file diff --git a/dist/updateIn.test.d.ts b/dist/updateIn.test.d.ts new file mode 100644 index 0000000..6a23d85 --- /dev/null +++ b/dist/updateIn.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updateIn.test.d.ts.map \ No newline at end of file diff --git a/dist/updateIn.test.d.ts.map b/dist/updateIn.test.d.ts.map new file mode 100644 index 0000000..6be947e --- /dev/null +++ b/dist/updateIn.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.test.d.ts","sourceRoot":"","sources":["../src/updateIn.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/updateIn.test.js b/dist/updateIn.test.js new file mode 100644 index 0000000..b376a79 --- /dev/null +++ b/dist/updateIn.test.js @@ -0,0 +1,52 @@ +import { expect, test, describe } from "vitest"; +import updateIn from "./updateIn.js"; +describe("updateIn", () => { + test("can update a single path described with a string", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("can update a single path described with a string with a function", () => { + const inc = (x) => x + 1; + const object = { a: { b: 0 } }; + const result = updateIn("a.b", inc)(object); + expect(result).to.eql({ a: { b: 1 } }); + }); + test("can update a single path described with an array", () => { + const object = { a: { b: 0 } }; + const result = updateIn(["a", "b"], 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("can update arrays", () => { + const object = { a: [0, 0, 0] }; + const result = updateIn("a.1", 3)(object); + expect(result).to.eql({ a: [0, 3, 0] }); + }); + test("can be partially applied", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("replaces the object outright if the path is empty", () => { + const object = {}; + const result = updateIn("", 3)(object); + expect(result).to.equal(3); + }); + test("freezes the result", () => { + expect(Object.isFrozen(updateIn("a", 0)({}))).to.be.true; + }); + test("can multiple elements of an array with *", () => { + let object = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] }; + let result = updateIn("a.*.b", (x) => x + 1)(object); + expect(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] }); + object = { a: [0, 1, 2] }; + result = updateIn(["a", "*"], (x) => x + 1)(object); + expect(result).to.eql({ a: [1, 2, 3] }); + }); + test("can update properties named *", () => { + const object = { "*": 1, x: 1 }; + const result = updateIn("*", (x) => x + 1)(object); + expect(result).to.eql({ "*": 2, x: 1 }); + }); +}); +//# sourceMappingURL=updateIn.test.js.map \ No newline at end of file diff --git a/dist/updateIn.test.js.map b/dist/updateIn.test.js.map new file mode 100644 index 0000000..e138999 --- /dev/null +++ b/dist/updateIn.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.test.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,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/updeep.bench.d.ts b/dist/updeep.bench.d.ts new file mode 100644 index 0000000..87237b2 --- /dev/null +++ b/dist/updeep.bench.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.bench.d.ts.map \ No newline at end of file diff --git a/dist/updeep.bench.d.ts.map b/dist/updeep.bench.d.ts.map new file mode 100644 index 0000000..393d6cf --- /dev/null +++ b/dist/updeep.bench.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.d.ts","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/updeep.bench.js b/dist/updeep.bench.js new file mode 100644 index 0000000..bcdfd75 --- /dev/null +++ b/dist/updeep.bench.js @@ -0,0 +1,10 @@ +import original from "updeep"; +import contender from "./index.js"; +import { bench } from "vitest"; +bench("original, simple update", () => { + original({ a: 1 })({ a: 2, b: 3 }); +}); +bench("contender, simple update", () => { + contender({ a: 1 })({ a: 2, b: 3 }); +}); +//# sourceMappingURL=updeep.bench.js.map \ No newline at end of file diff --git a/dist/updeep.bench.js.map b/dist/updeep.bench.js.map new file mode 100644 index 0000000..06af885 --- /dev/null +++ b/dist/updeep.bench.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.js","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,QAAQ,CAAC;AAC9B,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,KAAK,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACnC,QAAgB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,KAAK,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACrC,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/updeep.test.d.ts b/dist/updeep.test.d.ts new file mode 100644 index 0000000..c9a3e6b --- /dev/null +++ b/dist/updeep.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.test.d.ts.map \ No newline at end of file diff --git a/dist/updeep.test.d.ts.map b/dist/updeep.test.d.ts.map new file mode 100644 index 0000000..96f1294 --- /dev/null +++ b/dist/updeep.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.d.ts","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/updeep.test.js b/dist/updeep.test.js new file mode 100644 index 0000000..5622b61 --- /dev/null +++ b/dist/updeep.test.js @@ -0,0 +1,108 @@ +import { expect, it, describe } from "vitest"; +import u from "./index.js"; +it("does not change anything if no updates are specified", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, {}); + expect(result).to.equal(object); +}); +it("can update with fixed values", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, { foo: 4 }); + expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); +}); +it("returns the same instance if an update doesn't make changes", () => { + const object = { foo: 3 }; + const result = u({ foo: 3 })(object); + expect(result).to.equal(object); +}); +it("can update a nested structure", () => { + const object = { foo: { bar: 7, bam: 3 }, baz: 32 }; + const result = u({ foo: { bar: 8 } })(object); + expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); +}); +it("can update arrays", () => { + const object = [1, 2, 3]; + const result = u({ 1: 7 })(object); + expect(result).to.deep.equal([1, 7, 3]); +}); +it("replaces the object outright if updates are a constant", () => { + expect(u(3)({})).to.equal(3); + expect(u(null)({})).to.be.null; +}); +it("can add an element to an array", () => { + const object = []; + const result = u({ 0: 3 })(object); + expect(result).to.eql([3]); +}); +it("can update nested arrays", () => { + const object = { foo: [1, 2, 3], bar: 9 }; + const result = u({ foo: { 1: 7 } })(object); + expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); +}); +it("can use functions to update values", () => { + const inc = (i) => i + 1; + const object = { foo: 3, bar: 4, baz: 7 }; + const result = u({ foo: inc, bar: inc })(object); + expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); +}); +it("can be partially applied", () => { + const inc = (i) => i + 1; + const object = { foo: 3 }; + const incFoo = u({ foo: inc }); + const result = incFoo(object); + expect(result).to.deep.equal({ foo: 4 }); +}); +it("can update if the value is an array", () => { + const object = {}; + const result = u({ foo: [0, 1] })(object); + expect(result).to.deep.equal({ foo: [0, 1] }); +}); +it("can update when original object is undefined", () => { + const result = u({ foo: [0, 1] })(undefined); + expect(result).to.deep.equal({ foo: [0, 1] }); +}); +it("can take a function as the updater", () => { + const result = u((i) => i + 1)(7); + expect(result).to.eql(8); +}); +it("deeply freezes the result", () => { + const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }); + expect(Object.isFrozen(result)).to.be.true; + expect(Object.isFrozen(result.foo)).to.be.true; +}); +it("assigns null values", () => { + expect(u({ isNull: null }, {})).to.eql({ isNull: null }); +}); +it("defaults to an empty object when null or undefined", () => { + let result = u({ a: { b: 0 } })({ a: null }); + expect(result).to.eql({ a: { b: 0 } }); + result = u({ a: { b: 0 } })({ a: undefined }); + expect(result).to.eql({ a: { b: 0 } }); + result = u({ a: { b: 0 } })({}); + expect(result).to.eql({ a: { b: 0 } }); +}); +it("preserves empty objects when empty updates are specified", () => { + const result = u({ a: {} })({}); + expect(result).to.eql({ a: {} }); +}); +it("works with date objects", () => { + const date = new Date(); + const result = u({ created: date })({}); + expect(result).toEqual({ created: date }); +}); +const expectU = (update, orig, expected) => expect(update(orig)).to.eql(expected); +describe("u.omitted", () => { + it("omit properties via u.omitted", () => { + expectU(u({ a: u.omitted, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }); + }); + it("omit array and object properties", () => { + expectU(u({ a: u.omitted, b: "stuff", c: u.omitted }), { a: [1, 2, 3], b: "orig", c: { z: "bar" } }, { b: "stuff" }); + }); + it("deep omit", () => { + expectU(u({ a: { b: u.omitted, c: "stuff" } }), { a: { b: "foo", z: "bar" } }, { a: { z: "bar", c: "stuff" } }); + }); + it("omitting an array item filters it out", () => { + expectU(u({ 1: u.omitted }), ["a", "b", "c"], ["a", "c"]); + }); +}); +//# sourceMappingURL=updeep.test.js.map \ No newline at end of file diff --git a/dist/updeep.test.js.map b/dist/updeep.test.js.map new file mode 100644 index 0000000..2453ac0 --- /dev/null +++ b/dist/updeep.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.js","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,CAAC,MAAM,YAAY,CAAC;AAE3B,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;IAC9D,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IACvC,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAE7B,MAAM,CA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C,EAAE,GAAG,EAAE;QAC1C,OAAO,CACL,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAC7C,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAC5C,EAAE,CAAC,EAAE,OAAO,EAAE,CACf,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;QACnB,OAAO,CACL,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC,EACtC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAC7B,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAChC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/util/curry.d.ts b/dist/util/curry.d.ts new file mode 100644 index 0000000..d6daf4a --- /dev/null +++ b/dist/util/curry.d.ts @@ -0,0 +1,7 @@ +export function curry1(fn: any): (a: any, ...args: any[]) => any; +export function curry2(fn: any): (a: any, b: any, ...args: any[]) => any; +export function curry3(fn: any): (a: any, b: any, c: any, ...args: any[]) => any; +export function curry4(fn: any): (a: any, b: any, c: any, d: any, ...args: any[]) => any; +export default function curry(fn: any, length?: any): any; +export const _: "@@updeep/placeholder"; +//# sourceMappingURL=curry.d.ts.map \ No newline at end of file diff --git a/dist/util/curry.d.ts.map b/dist/util/curry.d.ts.map new file mode 100644 index 0000000..06c88c6 --- /dev/null +++ b/dist/util/curry.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"curry.d.ts","sourceRoot":"","sources":["../../src/util/curry.js"],"names":[],"mappings":"AAcA,iEAQC;AAED,yEAmBC;AAED,iFA6BC;AAED,yFA6CC;AAED,0DAEC;AA5HD,uCAAuC"} \ No newline at end of file diff --git a/dist/util/curry.js b/dist/util/curry.js new file mode 100644 index 0000000..82a7856 --- /dev/null +++ b/dist/util/curry.js @@ -0,0 +1,115 @@ +/* eslint no-shadow:0, no-param-reassign:0 prefer-rest-params:0 */ +export const _ = '@@updeep/placeholder'; +function countArguments(args, max) { + let n = args.length; + if (n > max) + n = max; + while (args[n - 1] === _) { + n -= 1; + } + return n; +} +export function curry1(fn) { + return function curried(a, ...args) { + const [b, c] = args; + const n = countArguments(arguments); + if (n >= 1) + return fn(a, b, c); + return curried; + }; +} +export function curry2(fn) { + return function curried(a, b, ...args) { + const [c, d] = args; + const n = countArguments(arguments, 2); + if (b === _ || c === _ || d === _) { + throw new Error('Can only use placeholder on first argument of this function.'); + } + if (n >= 2) { + if (a === _) + return curry1((a, c, d) => fn(a, b, c, d)); + return fn(a, b, c, d); + } + if (n === 1) + return curry1((b, c, d) => fn(a, b, c, d)); + return curried; + }; +} +export function curry3(fn) { + return function curried(a, b, c, ...args) { + const [d, e] = args; + const n = countArguments(arguments, 3); + if (c === _ || d === _ || e === _) { + throw new Error('Can only use placeholder on first or second argument of this function.'); + } + if (n >= 3) { + if (a === _) { + if (b === _) + return curry2((a, b, d, e) => fn(a, b, c, d, e)); + return curry1((a, d, e) => fn(a, b, c, d, e)); + } + if (b === _) + return curry1((b, d, e) => fn(a, b, c, d, e)); + return fn(a, b, c, d, e); + } + if (n === 2) { + if (a === _) + return curry2((a, c, d, e) => fn(a, b, c, d, e)); + return curry1((c, d, e) => fn(a, b, c, d, e)); + } + if (n === 1) + return curry2((b, c, d, e) => fn(a, b, c, d, e)); + return curried; + }; +} +export function curry4(fn) { + return function curried(a, b, c, d, ...args) { + const [e, f] = args; + const n = countArguments(arguments, 4); + if (d === _ || e === _ || f === _) { + throw new Error('Can only use placeholder on first, second or third argument of this function.'); + } + if (n >= 4) { + if (a === _) { + if (b === _) { + if (c === _) + return curry3((a, b, c, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === _) + return curry2((a, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((a, e, f) => fn(a, b, c, d, e, f)); + } + if (b === _) { + if (c === _) + return curry2((b, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === _) + return curry1((c, e, f) => fn(a, b, c, d, e, f)); + return fn(a, b, c, d, e, f); + } + if (n === 3) { + if (a === _) { + if (b === _) + return curry3((a, b, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, d, e, f) => fn(a, b, c, d, e, f)); + } + if (b === _) + return curry2((b, d, e, f) => fn(a, b, c, d, e, f)); + return curry1((d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 2) { + if (a === _) + return curry3((a, c, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((c, d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 1) + return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f)); + return curried; + }; +} +export default function curry(fn, length = fn.length) { + return [fn, curry1, curry2, curry3, curry4][length](fn); +} +//# sourceMappingURL=curry.js.map \ No newline at end of file diff --git a/dist/util/curry.js.map b/dist/util/curry.js.map new file mode 100644 index 0000000..5b3b687 --- /dev/null +++ b/dist/util/curry.js.map @@ -0,0 +1 @@ +{"version":3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d.ts b/dist/util/splitPath.d.ts new file mode 100644 index 0000000..6daeb5e --- /dev/null +++ b/dist/util/splitPath.d.ts @@ -0,0 +1,2 @@ +export default function splitPath(path: string | number | (number | string)[]): (string | number)[]; +//# sourceMappingURL=splitPath.d.ts.map \ No newline at end of file diff --git a/dist/util/splitPath.d.ts.map b/dist/util/splitPath.d.ts.map new file mode 100644 index 0000000..686b455 --- /dev/null +++ b/dist/util/splitPath.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,uBAM5E"} \ No newline at end of file diff --git a/dist/util/splitPath.js b/dist/util/splitPath.js new file mode 100644 index 0000000..b1adbff --- /dev/null +++ b/dist/util/splitPath.js @@ -0,0 +1,8 @@ +export default function splitPath(path) { + if (typeof path === "number") + path = new String(path); + return Array.isArray(path) + ? path + : path.split(".").filter((x) => x !== ""); +} +//# sourceMappingURL=splitPath.js.map \ No newline at end of file diff --git a/dist/util/splitPath.js.map b/dist/util/splitPath.js.map new file mode 100644 index 0000000..65caef2 --- /dev/null +++ b/dist/util/splitPath.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.js","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAA2C;IAC3E,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAQ,CAAC;IAE7D,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACxB,CAAC,CAAC,IAAI;QACN,CAAC,CAAE,IAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;AAC1D,CAAC"} \ No newline at end of file diff --git a/dist/util/splitPath.test.d.ts b/dist/util/splitPath.test.d.ts new file mode 100644 index 0000000..daa51d7 --- /dev/null +++ b/dist/util/splitPath.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=splitPath.test.d.ts.map \ No newline at end of file diff --git a/dist/util/splitPath.test.d.ts.map b/dist/util/splitPath.test.d.ts.map new file mode 100644 index 0000000..2866cd6 --- /dev/null +++ b/dist/util/splitPath.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/dist/util/splitPath.test.js b/dist/util/splitPath.test.js new file mode 100644 index 0000000..0b53b4e --- /dev/null +++ b/dist/util/splitPath.test.js @@ -0,0 +1,18 @@ +import { expect, it } from "vitest"; +import splitPath from "./splitPath.js"; +it("treats a number as a single step path", () => { + const path = 1; + const result = splitPath(path); + expect(result).to.deep.equal(["1"]); +}); +it("handles arrays", () => { + const path = ["foo", "bar", "x"]; + const result = splitPath(path); + expect(result).to.equal(path); +}); +it("handles strings separated by dots", () => { + const path = "bar.0.y"; + const result = splitPath(path); + expect(result).to.deep.equal(["bar", "0", "y"]); +}); +//# sourceMappingURL=splitPath.test.js.map \ No newline at end of file diff --git a/dist/util/splitPath.test.js.map b/dist/util/splitPath.test.js.map new file mode 100644 index 0000000..f2ae275 --- /dev/null +++ b/dist/util/splitPath.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.js","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;IAC/C,MAAM,IAAI,GAAG,CAAC,CAAC;IACf,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;IACxB,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC3C,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAClD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/wrap.d.ts b/dist/wrap.d.ts new file mode 100644 index 0000000..aa294b7 --- /dev/null +++ b/dist/wrap.d.ts @@ -0,0 +1,3 @@ +declare const wrap: (fn: any) => (...args: any[]) => Readonly; +export default wrap; +//# sourceMappingURL=wrap.d.ts.map \ No newline at end of file diff --git a/dist/wrap.d.ts.map b/dist/wrap.d.ts.map new file mode 100644 index 0000000..5061f46 --- /dev/null +++ b/dist/wrap.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,IAAI,gDAWP,CAAC;AAEJ,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/dist/wrap.js b/dist/wrap.js new file mode 100644 index 0000000..c106306 --- /dev/null +++ b/dist/wrap.js @@ -0,0 +1,13 @@ +import freeze from "./freeze.js"; +const wrap = (fn) => (...args) => { + const diff = fn.length - args.length; + if (diff === 0) { + return freeze(fn(...args)); + } + if (diff === 1) { + return (data) => freeze(fn(data, ...args)); + } + throw new Error("Wrong number of arguments"); +}; +export default wrap; +//# sourceMappingURL=wrap.js.map \ No newline at end of file diff --git a/dist/wrap.js.map b/dist/wrap.js.map new file mode 100644 index 0000000..2248993 --- /dev/null +++ b/dist/wrap.js.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.js","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,IAAI,GACR,CAAC,EAAE,EAAE,EAAE,CACP,CAAC,GAAG,IAAI,EAAE,EAAE;IACV,MAAM,IAAI,GAAG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5B;IACD,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5C;IACD,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;AAC/C,CAAC,CAAC;AAEJ,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/esm/constant.d.ts b/esm/constant.d.ts new file mode 100644 index 0000000..0e17823 --- /dev/null +++ b/esm/constant.d.ts @@ -0,0 +1,34 @@ +/** + * Returns a function that always returns the supplied value. + * + * Useful for replacing an object outright rather than merging it. + * + * @function + * @sig a -> (* -> a) + * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. + * + * @example + * var alwaysFour = u.constant(4); + * expect(alwaysFour(32)).toEqual(4); + * + * @example + * var user = { + * name: 'Mitch', + * favorites: { + * band: 'Nirvana', + * movie: 'The Matrix' + * } + * }; + * + * var newFavorites = { + * band: 'Coldplay' + * }; + * + * var result = u({ favorites: u.constant(newFavorites) }, user); + * + * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); + */ +export default function constant(value: T): () => Readonly; +//# sourceMappingURL=constant.d.ts.map \ No newline at end of file diff --git a/esm/constant.d.ts.map b/esm/constant.d.ts.map new file mode 100644 index 0000000..5a1fb8b --- /dev/null +++ b/esm/constant.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,qBAG3C"} \ No newline at end of file diff --git a/esm/constant.js b/esm/constant.js new file mode 100644 index 0000000..45fb1d3 --- /dev/null +++ b/esm/constant.js @@ -0,0 +1,38 @@ +import freeze from "./freeze.js"; +/** + * Returns a function that always returns the supplied value. + * + * Useful for replacing an object outright rather than merging it. + * + * @function + * @sig a -> (* -> a) + * @memberOf u + * @param {*} value what to return from returned function. + * @return {function} a new function that will always return value. + * + * @example + * var alwaysFour = u.constant(4); + * expect(alwaysFour(32)).toEqual(4); + * + * @example + * var user = { + * name: 'Mitch', + * favorites: { + * band: 'Nirvana', + * movie: 'The Matrix' + * } + * }; + * + * var newFavorites = { + * band: 'Coldplay' + * }; + * + * var result = u({ favorites: u.constant(newFavorites) }, user); + * + * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); + */ +export default function constant(value) { + const frozen = freeze(value); + return () => frozen; +} +//# sourceMappingURL=constant.js.map \ No newline at end of file diff --git a/esm/constant.js.map b/esm/constant.js.map new file mode 100644 index 0000000..0fe2057 --- /dev/null +++ b/esm/constant.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.js","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAI,KAAQ;IAC1C,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC"} \ No newline at end of file diff --git a/esm/constant.test.d.ts b/esm/constant.test.d.ts new file mode 100644 index 0000000..7a91645 --- /dev/null +++ b/esm/constant.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=constant.test.d.ts.map \ No newline at end of file diff --git a/esm/constant.test.d.ts.map b/esm/constant.test.d.ts.map new file mode 100644 index 0000000..9efc588 --- /dev/null +++ b/esm/constant.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.d.ts","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/constant.test.js b/esm/constant.test.js new file mode 100644 index 0000000..556615e --- /dev/null +++ b/esm/constant.test.js @@ -0,0 +1,13 @@ +import { expect, test } from "vitest"; +import constant from "./constant.js"; +test("returns what it is given... constantly", () => { + const func = constant(4); + expect(func()).to.equal(4); + expect(func("hi")).to.equal(4); + expect(func("hi", 8)).to.equal(4); + expect(func(4)).to.equal(4); +}); +test("freezes the result", () => { + expect(Object.isFrozen(constant({})())).toBeTruthy(); +}); +//# sourceMappingURL=constant.test.js.map \ No newline at end of file diff --git a/esm/constant.test.js.map b/esm/constant.test.js.map new file mode 100644 index 0000000..ea57055 --- /dev/null +++ b/esm/constant.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constant.test.js","sourceRoot":"","sources":["../src/constant.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,IAAI,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE;IAChD,MAAM,IAAI,GAAQ,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE9B,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC/B,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;IAC5B,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AACzD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/freeze.d.ts b/esm/freeze.d.ts new file mode 100644 index 0000000..7ae399c --- /dev/null +++ b/esm/freeze.d.ts @@ -0,0 +1,19 @@ +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +declare function freeze(object: T): Readonly; +export default freeze; +//# sourceMappingURL=freeze.d.ts.map \ No newline at end of file diff --git a/esm/freeze.d.ts.map b/esm/freeze.d.ts.map new file mode 100644 index 0000000..6057a76 --- /dev/null +++ b/esm/freeze.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.d.ts","sourceRoot":"","sources":["../src/freeze.ts"],"names":[],"mappings":"AAyBA;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAkBzC;AAED,eAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/esm/freeze.js b/esm/freeze.js new file mode 100644 index 0000000..50a9726 --- /dev/null +++ b/esm/freeze.js @@ -0,0 +1,55 @@ +function isFreezable(object) { + if (object === null) + return false; + if (object instanceof RegExp) + return false; + if (object instanceof ArrayBuffer) + return false; + return Array.isArray(object) || typeof object === "object"; +} +function needsFreezing(object) { + return isFreezable(object) && !Object.isFrozen(object); +} +function recur(object) { + Object.freeze(object); + Object.keys(object).forEach((key) => { + const value = object[key]; + if (needsFreezing(value)) { + recur(value); + } + }); + return object; +} +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +function freeze(object) { + if (typeof process === "undefined") { + return object; + } + if (process.env.NODE_ENV === "production") { + return object; + } + if (process.env.UPDEEP_MODE === "dangerously_never_freeze") { + return object; + } + if (needsFreezing(object)) { + recur(object); + } + return object; +} +export default freeze; +//# sourceMappingURL=freeze.js.map \ No newline at end of file diff --git a/esm/freeze.js.map b/esm/freeze.js.map new file mode 100644 index 0000000..c1cb063 --- /dev/null +++ b/esm/freeze.js.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.js","sourceRoot":"","sources":["../src/freeze.ts"],"names":[],"mappings":"AAAA,SAAS,WAAW,CAAC,MAAe;IAClC,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAClC,IAAI,MAAM,YAAY,MAAM;QAAE,OAAO,KAAK,CAAC;IAC3C,IAAI,MAAM,YAAY,WAAW;QAAE,OAAO,KAAK,CAAC;IAEhD,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,MAAM,KAAK,QAAQ,CAAC;AAC7D,CAAC;AAED,SAAS,aAAa,CAAC,MAAe;IACpC,OAAO,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACzD,CAAC;AAED,SAAS,KAAK,CAAC,MAAM;IACnB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAEtB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAClC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE;YACxB,KAAK,CAAC,KAAK,CAAC,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAS,MAAM,CAAI,MAAS;IAC1B,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QAClC,OAAO,MAAM,CAAC;KACf;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,OAAO,MAAM,CAAC;KACf;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,KAAK,0BAA0B,EAAE;QAC1D,OAAO,MAAM,CAAC;KACf;IAED,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE;QACzB,KAAK,CAAC,MAAM,CAAC,CAAC;KACf;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,eAAe,MAAM,CAAC"} \ No newline at end of file diff --git a/esm/freeze.test.d.ts b/esm/freeze.test.d.ts new file mode 100644 index 0000000..ecc5984 --- /dev/null +++ b/esm/freeze.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=freeze.test.d.ts.map \ No newline at end of file diff --git a/esm/freeze.test.d.ts.map b/esm/freeze.test.d.ts.map new file mode 100644 index 0000000..07a38c1 --- /dev/null +++ b/esm/freeze.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"freeze.test.d.ts","sourceRoot":"","sources":["../src/freeze.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/freeze.test.js b/esm/freeze.test.js new file mode 100644 index 0000000..2ba34fa --- /dev/null +++ b/esm/freeze.test.js @@ -0,0 +1,56 @@ +import { describe, afterEach, test, expect } from "vitest"; +import freeze from "./freeze.js"; +describe("freeze", () => { + afterEach(() => { + delete process.env.NODE_ENV; + }); + test("freezes objects", () => { + const object = {}; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + }); + test("freezes nested objects", () => { + const object = { foo: { bar: 3 } }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.true; + }); + test("freezes nested arrays", () => { + const object = [[0]]; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + expect(Object.isFrozen(object[0])).to.be.true; + }); + test("ignores functions", () => { + const object = { foo: () => 1 }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("ignores regexps", () => { + const object = { foo: /\d/ }; + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("does not freeze children if the parent is already frozen", () => { + const object = { foo: {} }; + Object.freeze(object); + freeze(object); + expect(Object.isFrozen(object.foo)).to.be.false; + }); + test("does not freeze in production", () => { + process.env.NODE_ENV = "production"; + const object = {}; + freeze(object); + expect(Object.isFrozen(object)).to.be.false; + }); + test("handles null objects", () => { + const object = { foo: null }; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + }); + test("returns the same object", () => { + const object = {}; + const result = freeze(object); + expect(result).to.equal(object); + }); +}); +//# sourceMappingURL=freeze.test.js.map \ No newline at end of file diff --git a/esm/freeze.test.js.map b/esm/freeze.test.js.map new file mode 100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Q,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAChC,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAC7C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACnC,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/if.d.ts b/esm/if.d.ts new file mode 100644 index 0000000..f11eafc --- /dev/null +++ b/esm/if.d.ts @@ -0,0 +1,7 @@ +interface If { + (object: any, predicate: any, trueUpdates: any): unknown; + (predicate: any, trueUpdates: any): (unknown: any) => unknown; +} +declare const _default: If; +export default _default; +//# sourceMappingURL=if.d.ts.map \ No newline at end of file diff --git a/esm/if.d.ts.map b/esm/if.d.ts.map new file mode 100644 index 0000000..af7f764 --- /dev/null +++ b/esm/if.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.d.ts","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":"AAKA,UAAU,EAAE;IACV,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,OAAO,CAAC;IAC1C,CAAC,SAAS,KAAA,EAAE,WAAW,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CAChD;;AAMD,wBAA+B"} \ No newline at end of file diff --git a/esm/if.js b/esm/if.js new file mode 100644 index 0000000..709f226 --- /dev/null +++ b/esm/if.js @@ -0,0 +1,8 @@ +import { identity } from "remeda"; +import ifElse from "./ifElse.js"; +import wrap from "./wrap.js"; +function _if(object, predicate, trueUpdates) { + return ifElse(object, predicate, trueUpdates, identity); +} +export default wrap(_if); +//# sourceMappingURL=if.js.map \ No newline at end of file diff --git a/esm/if.js.map b/esm/if.js.map new file mode 100644 index 0000000..0491bde --- /dev/null +++ b/esm/if.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.js","sourceRoot":"","sources":["../src/if.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAO7B,SAAS,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW;IACzC,OAAO,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAC;AAC1D,CAAC;AAED,eAAe,IAAI,CAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/esm/if.test.d.ts b/esm/if.test.d.ts new file mode 100644 index 0000000..d034164 --- /dev/null +++ b/esm/if.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=if.test.d.ts.map \ No newline at end of file diff --git a/esm/if.test.d.ts.map b/esm/if.test.d.ts.map new file mode 100644 index 0000000..1d2dd89 --- /dev/null +++ b/esm/if.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.d.ts","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/if.test.js b/esm/if.test.js new file mode 100644 index 0000000..588d675 --- /dev/null +++ b/esm/if.test.js @@ -0,0 +1,27 @@ +import { describe, test, expect } from "vitest"; +import _if from "./if.js"; +describe("if", () => { + test("does not update if the predicate is false", () => { + const object = { a: 0 }; + let result = _if(false, { b: 1 }, object); + expect(result).to.eql(object); + result = _if(0, false, 1); + expect(result).to.eql(0); + }); + test("does update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = _if(aIsThree, { b: 1 }, object); + expect(result).to.eql({ a: 0 }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(_if(true, {})({}))).to.be.true; + expect(Object.isFrozen(_if(false, {})({}))).to.be.true; + }); +}); +//# sourceMappingURL=if.test.js.map \ No newline at end of file diff --git a/esm/if.test.js.map b/esm/if.test.js.map new file mode 100644 index 0000000..3b765a8 --- /dev/null +++ b/esm/if.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"if.test.js","sourceRoot":"","sources":["../src/if.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,GAAG,MAAM,SAAS,CAAC;AAE1B,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE;IAClB,IAAI,CAAC,2CAA2C,EAAE,GAAG,EAAE;QACrD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,IAAI,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QAC1C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAE9B,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QAC1B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sCAAsC,EAAE,GAAG,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 b/esm/ifElse.d.ts @@ -0,0 +1,8 @@ +type Predicate = ((source: any) => boolean) | boolean; +interface IfElse { + (object: any, predicate: Predicate, trueUpdates: any, falseUpdates: any): unknown; + (predicate: Predicate, trueUpdates: any, falseUpdates: any): (unknown: any) => unknown; +} +declare const _default: IfElse; +export default _default; +//# sourceMappingURL=ifElse.d.ts.map \ No newline at end of file diff --git a/esm/ifElse.d.ts.map b/esm/ifElse.d.ts.map new file mode 100644 index 0000000..c085b20 --- /dev/null +++ b/esm/ifElse.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.d.ts","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":"AAWA,KAAK,SAAS,GAAG,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,OAAO,CAAC;AAEtD,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,OAAO,CAAC;IACnE,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,KAAA,EAAE,YAAY,KAAA,GAAG,CAAC,OAAO,KAAA,KAAK,OAAO,CAAC;CACzE;;AAED,wBAA4C"} \ No newline at end of file diff --git a/esm/ifElse.js b/esm/ifElse.js new file mode 100644 index 0000000..f353854 --- /dev/null +++ b/esm/ifElse.js @@ -0,0 +1,9 @@ +import update from "./update.js"; +import wrap from "./wrap.js"; +function updateIfElse(object, predicate, trueUpdates, falseUpdates) { + const test = typeof predicate === "function" ? predicate(object) : predicate; + const updates = test ? trueUpdates : falseUpdates; + return update(object, updates); +} +export default wrap(updateIfElse); +//# sourceMappingURL=ifElse.js.map \ No newline at end of file diff --git a/esm/ifElse.js.map b/esm/ifElse.js.map new file mode 100644 index 0000000..8287535 --- /dev/null +++ b/esm/ifElse.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.js","sourceRoot":"","sources":["../src/ifElse.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY;IAChE,MAAM,IAAI,GAAG,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE7E,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;IAElD,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AASD,eAAe,IAAI,CAAC,YAAY,CAAW,CAAC"} \ No newline at end of file diff --git a/esm/ifElse.test.d.ts b/esm/ifElse.test.d.ts new file mode 100644 index 0000000..d8022ab --- /dev/null +++ b/esm/ifElse.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=ifElse.test.d.ts.map \ No newline at end of file diff --git a/esm/ifElse.test.d.ts.map b/esm/ifElse.test.d.ts.map new file mode 100644 index 0000000..6ed44d0 --- /dev/null +++ b/esm/ifElse.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.d.ts","sourceRoot":"","sources":["../src/ifElse.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/ifElse.test.js b/esm/ifElse.test.js new file mode 100644 index 0000000..4902f57 --- /dev/null +++ b/esm/ifElse.test.js @@ -0,0 +1,38 @@ +import { expect, test, describe } from "vitest"; +import ifElse from "./ifElse.js"; +import _if from "./if.js"; +describe("ifElse", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = ifElse(false, { b: 1 }, { b: 2 })(object); + expect(result).to.eql({ a: 0, b: 2 }); + }); + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = ifElse(true, { b: 1 }, { b: 4 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = ifElse(aIsThree, { b: 1 }, { b: 4 })(object); + expect(result).to.eql({ a: 0, b: 4 }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(ifElse(true, {}, {})({}))).to.be.true; + expect(Object.isFrozen(ifElse(false, {}, {})({}))).to.be.true; + }); +}); +describe("if", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = _if(false, { b: 1 })(object); + expect(result).not.toHaveProperty("b"); + }); + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); +}); +//# sourceMappingURL=ifElse.test.js.map \ No newline at end of file diff --git a/esm/ifElse.test.js.map b/esm/ifElse.test.js.map new file mode 100644 index 0000000..de91212 --- /dev/null +++ b/esm/ifElse.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"ifElse.test.js","sourceRoot":"","sources":["../src/ifElse.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,GAAG,MAAM,SAAS,CAAC;AAE1B,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAI,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAChE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IA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E,CAAC,EAAE,CAAC,IAAI,CAAC;IAChE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE;IAClB,IAAI,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAChE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC5C,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uDAAuD,EAAE,GAAG,EAAE;QACjE,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC3C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/index.d.ts b/esm/index.d.ts new file mode 100644 index 0000000..49c130d --- /dev/null +++ b/esm/index.d.ts @@ -0,0 +1,3 @@ +declare const u: any; +export default u; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/esm/index.d.ts.map b/esm/index.d.ts.map new file mode 100644 index 0000000..e29798d --- /dev/null +++ b/esm/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,QAAA,MAAM,CAAC,EAAE,GAAY,CAAC;AAWtB,eAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/index.js b/esm/index.js new file mode 100644 index 0000000..0ba381d --- /dev/null +++ b/esm/index.js @@ -0,0 +1,18 @@ +import freeze from "./freeze.js"; +import is from "./is.js"; +import _if from "./if.js"; +import ifElse from "./ifElse.js"; +import update, { omitted } from "./update.js"; +import updateIn from "./updateIn.js"; +import constant from "./constant.js"; +const u = update; +u.constant = constant; +u.if = _if; +u.ifElse = ifElse; +u.is = is; +u.freeze = freeze; +u.update = update; +u.updateIn = updateIn; +u.omitted = omitted; +export default u; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/esm/index.js.map b/esm/index.js.map new file mode 100644 index 0000000..5cae3d4 --- /dev/null +++ b/esm/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,GAAG,MAAM,SAAS,CAAC;AAC1B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,CAAC,GAAQ,MAAM,CAAC;AAEtB,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACtB,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC;AACX,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;AACV,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;AAClB,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACtB,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC;AAEpB,eAAe,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/is.d.ts b/esm/is.d.ts new file mode 100644 index 0000000..bb1ddfd --- /dev/null +++ b/esm/is.d.ts @@ -0,0 +1,7 @@ +interface Is { + (object: any, path: string | number | (number | string)[], predicate: any): boolean; + (path: string | number | (number | string)[], predicate: any): (object: any) => boolean; +} +declare const _default: Is; +export default _default; +//# sourceMappingURL=is.d.ts.map \ No newline at end of file diff --git a/esm/is.d.ts.map b/esm/is.d.ts.map new file mode 100644 index 0000000..25c05b7 --- /dev/null +++ b/esm/is.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.d.ts","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":"AAsBA,UAAU,EAAE;IACR,CAAC,MAAM,KAAA,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,OAAO,CAAC;IAC1E,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,OAAO,CAAC;CACjF;;AAED,wBAA+B"} \ No newline at end of file diff --git a/esm/is.js b/esm/is.js new file mode 100644 index 0000000..ea27999 --- /dev/null +++ b/esm/is.js @@ -0,0 +1,16 @@ +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; +function _is(object, path, predicate) { + const parts = splitPath(path); + for (const part of parts) { + if (typeof object === "undefined") + return false; + object = object[part]; + } + if (typeof predicate === "function") { + return predicate(object); + } + return predicate === object; +} +export default wrap(_is); +//# sourceMappingURL=is.js.map \ No newline at end of file diff --git a/esm/is.js.map b/esm/is.js.map new file mode 100644 index 0000000..fc045d3 --- /dev/null +++ b/esm/is.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.js","sourceRoot":"","sources":["../src/is.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,GAAG,CACR,MAAM,EACN,IAA2C,EAC3C,SAAS;IAET,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,IAAI,OAAO,MAAM,KAAK,WAAW;YAAE,OAAO,KAAK,CAAC;QAChD,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;KACzB;IAED,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;QACjC,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;KAC5B;IAED,OAAO,SAAS,KAAK,MAAM,CAAC;AAChC,CAAC;AAOD,eAAe,IAAI,CAAC,GAAG,CAAO,CAAC"} \ No newline at end of file diff --git a/esm/is.test.d.ts b/esm/is.test.d.ts new file mode 100644 index 0000000..ed225e5 --- /dev/null +++ b/esm/is.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=is.test.d.ts.map \ No newline at end of file diff --git a/esm/is.test.d.ts.map b/esm/is.test.d.ts.map new file mode 100644 index 0000000..1e7cd13 --- /dev/null +++ b/esm/is.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.d.ts","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/is.test.js b/esm/is.test.js new file mode 100644 index 0000000..e3ea792 --- /dev/null +++ b/esm/is.test.js @@ -0,0 +1,47 @@ +import { expect, test } from "vitest"; +import is from "./is.js"; +test("returns true if path matches a value predicate", () => { + const result = is({ a: { b: 4 } }, "a.b", 4); + expect(result).to.be.true; +}); +test("returns true if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 6 } }); + expect(result).to.be.true; +}); +test("returns false if path matches a value predicate", () => { + const result = is("a.b", 4)({ a: { b: 5 } }); + expect(result).to.be.false; +}); +test("returns false if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 7 } }); + expect(result).to.be.false; +}); +test("returns false if the path does not exist", () => { + const result = is("a.b.c.d", 4)({ a: { b: {} } }); + expect(result).to.be.false; +}); +test("can test for undefined", () => { + const result = is("a.b.c", undefined)({ a: { b: {} } }); + expect(result).to.be.true; +}); +test("tests the actual object if a blank path is given", () => { + const result = is(4, "", 4); + expect(result).toBeTruthy(); +}); +test("can use arrays as paths", () => { + const result = is(["a", "b"], 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); +test("can include array indexes in paths", () => { + let result = is("a.1.b", 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; + result = is(["a", 1, "b"], 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; +}); +test("can be partially applied", () => { + const result = is("a.b", 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); +//# sourceMappingURL=is.test.js.map \ No newline at end of file diff --git a/esm/is.test.js.map b/esm/is.test.js.map new file mode 100644 index 0000000..5eb5008 --- /dev/null +++ b/esm/is.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"is.test.js","sourceRoot":"","sources":["../src/is.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,IAAI,CAAC,gDAAgD,EAAE,GAAG,EAAE;IACxD,MAAM,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAE7C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mDAAmD,EAAE,GAAG,EAAE;IAC3D,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,EAAE,CAAC,KAA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A0B,EAAE,GAAG,EAAE;IAClC,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;AAC9B,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/map.d.ts b/esm/map.d.ts new file mode 100644 index 0000000..a4e9f5a --- /dev/null +++ b/esm/map.d.ts @@ -0,0 +1,7 @@ +interface Map { + (object: any, iteratee: any): any; + (iteratee: any): (object: any) => any; +} +declare const _default: Map; +export default _default; +//# sourceMappingURL=map.d.ts.map \ No newline at end of file diff --git a/esm/map.d.ts.map b/esm/map.d.ts.map new file mode 100644 index 0000000..f7f85d8 --- /dev/null +++ b/esm/map.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAuBA,UAAU,GAAG;IACX,CAAC,MAAM,KAAA,EAAE,QAAQ,KAAA,GAAG,GAAG,CAAC;IACxB,CAAC,QAAQ,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAC7B;;AAED,wBAAgC"} \ No newline at end of file diff --git a/esm/map.js b/esm/map.js new file mode 100644 index 0000000..b12e7b9 --- /dev/null +++ b/esm/map.js @@ -0,0 +1,19 @@ +import { mapValues, map as _map } from "remeda"; +import update from "./update.js"; +import wrap from "./wrap.js"; +function shallowEqual(object, otherObject) { + for (const k in otherObject) { + if (otherObject[k] !== object[k]) + return false; + } + return true; +} +function map(object, iteratee) { + const updater = typeof iteratee === "function" ? iteratee : update(iteratee); + const mapper = Array.isArray(object) ? _map.indexed : mapValues; + const newObject = mapper(object, updater); + const equal = shallowEqual(object, newObject); + return equal ? object : newObject; +} +export default wrap(map); +//# sourceMappingURL=map.js.map \ No newline at end of file diff --git a/esm/map.js.map b/esm/map.js.map new file mode 100644 index 0000000..a7530fc --- /dev/null +++ b/esm/map.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,GAAG,IAAI,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,YAAY,CAAC,MAAM,EAAE,WAAW;IACvC,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE;QAC3B,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;KAChD;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,GAAG,CAAC,MAAM,EAAE,QAAQ;IAC3B,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAE7E,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAEhE,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACpC,CAAC;AAOD,eAAe,IAAI,CAAC,GAAG,CAAQ,CAAC"} \ No newline at end of file diff --git a/esm/map.test.d.ts b/esm/map.test.d.ts new file mode 100644 index 0000000..3be85f6 --- /dev/null +++ b/esm/map.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=map.test.d.ts.map \ No newline at end of file diff --git a/esm/map.test.d.ts.map b/esm/map.test.d.ts.map new file mode 100644 index 0000000..ef09e8b --- /dev/null +++ b/esm/map.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.d.ts","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/map.test.js b/esm/map.test.js new file mode 100644 index 0000000..c4813ab --- /dev/null +++ b/esm/map.test.js @@ -0,0 +1,46 @@ +import { expect, test, describe } from "vitest"; +import map from "./map.js"; +describe("map", () => { + test("applies updates to each item in an array", () => { + const object = [0, 1, 2]; + const inc = (x) => x + 1; + const result = map(object, inc); + expect(result).to.eql([1, 2, 3]); + }); + test("applies updates to each value in an object", () => { + const object = { a: 0, b: 1, c: 2 }; + const inc = (x) => x + 1; + const result = map(inc)(object); + expect(result).to.eql({ a: 1, b: 2, c: 3 }); + }); + test("can update with a regular updates object", () => { + const object = [{ a: 0 }, { a: 0 }]; + const result = map({ a: 1 })(object); + expect(result).to.eql([{ a: 1 }, { a: 1 }]); + }); + test("returns the same object if no updates are made", () => { + const array = [0, 1]; + const ident = (x) => x; + let result = map(ident)(array); + expect(result).to.equal(array); + const object = { a: 0 }; + result = map(ident)(object); + expect(result).to.equal(object); + }); + test("passes the key or index as the second parameter to the iteratee", () => { + const object = { + a: { x: 0 }, + b: [3, 3], + }; + const setToKey = (_, key) => key; + const result = map(object, map(setToKey)); + expect(result).to.eql({ + a: { x: "x" }, + b: [0, 1], + }); + }); + test("freezes the result", () => { + expect(Object.isFrozen(map({}, {}))).to.be.true; + }); +}); +//# sourceMappingURL=map.test.js.map \ No newline at end of file diff --git a/esm/map.test.js.map b/esm/map.test.js.map new file mode 100644 index 0000000..54bc0e7 --- /dev/null +++ b/esm/map.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"map.test.js","sourceRoot":"","sources":["../src/map.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC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CAAC,CAAC;IAEH,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAC9B,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/update.d.ts b/esm/update.d.ts new file mode 100644 index 0000000..8c488f3 --- /dev/null +++ b/esm/update.d.ts @@ -0,0 +1,10 @@ +export declare const omitted: () => Readonly<{ + __omitted: boolean; +}>; +interface Update { + (object: any, func: any): any; + (func: any): (object: any) => any; +} +declare const _default: Update; +export default _default; +//# sourceMappingURL=update.d.ts.map \ No newline at end of file diff --git a/esm/update.d.ts.map b/esm/update.d.ts.map new file mode 100644 index 0000000..d6c776f --- /dev/null +++ b/esm/update.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO;;EAAyB,CAAC;AAkG9C,UAAU,MAAM;IACd,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,GAAG,GAAG,CAAC;IACpB,CAAC,IAAI,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CACzB;;AAED,wBAAsC"} \ No newline at end of file diff --git a/esm/update.js b/esm/update.js new file mode 100644 index 0000000..e3dfaa7 --- /dev/null +++ b/esm/update.js @@ -0,0 +1,72 @@ +import wrap from "./wrap.js"; +import constant from "./constant.js"; +import { omitBy } from "remeda"; +const innerOmitted = { __omitted: true }; +export const omitted = constant(innerOmitted); +function isEmpty(object) { + return !Object.keys(object).length; +} +function reduce(object, callback, initialValue) { + return Object.keys(object).reduce((acc, key) => callback(acc, object[key], key), initialValue); +} +function resolveUpdates(updates, object) { + return reduce(updates, (acc, value, key) => { + let updatedValue = value; + if (!Array.isArray(value) && + value !== null && + typeof value === "object") { + updatedValue = update(object[key], value); // eslint-disable-line no-use-before-define + } + else if (typeof value === "function") { + updatedValue = value(object[key]); + } + if (object[key] !== updatedValue) { + acc[key] = updatedValue; // eslint-disable-line no-param-reassign + } + return acc; + }, {}); +} +function updateArray(updates, object) { + const newArray = [...object]; + Object.keys(updates).forEach((key) => { + newArray[key] = updates[key]; + }); + return newArray; +} +const isPlainObject = (value) => (value === null || value === void 0 ? void 0 : value.constructor) === Object; +/** + * Recursively update an object or array. + * + * Can update with values: + * update({ foo: 3 }, { foo: 1, bar: 2 }); + * // => { foo: 3, bar: 2 } + * + * Or with a function: + * update({ foo: x => (x + 1) }, { foo: 2 }); + * // => { foo: 3 } + * + * @function + * @name update + * @param {Object|Function} updates + * @param {Object|Array} object to update + * @return {Object|Array} new object with modifications + */ +function update(object, updates) { + if (typeof updates === "function") { + return updates(object); + } + if (!isPlainObject(updates)) { + return updates; + } + const defaultedObject = typeof object === "undefined" || object === null ? {} : object; + const resolvedUpdates = resolveUpdates(updates, defaultedObject); + if (isEmpty(resolvedUpdates)) { + return defaultedObject; + } + if (Array.isArray(defaultedObject)) { + return updateArray(resolvedUpdates, defaultedObject).filter((value) => value !== innerOmitted); + } + return omitBy(Object.assign(Object.assign({}, defaultedObject), resolvedUpdates), (value) => value === innerOmitted); +} +export default wrap(update); +//# sourceMappingURL=update.js.map \ No newline at end of file diff --git a/esm/update.js.map b/esm/update.js.map new file mode 100644 index 0000000..3bab5f6 --- /dev/null +++ b/esm/update.js.map @@ -0,0 +1 @@ +{"version":3,"file":"update.js","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,OAAO,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAE1C,MAAM,YAAY,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;AACzC,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,C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.d.ts @@ -0,0 +1,7 @@ +interface UpdateIn { + (object: any, path: any, value: any): any; + (path: any, value: any): (object: any) => any; +} +declare const _default: UpdateIn; +export default _default; +//# sourceMappingURL=updateIn.d.ts.map \ No newline at end of file diff --git a/esm/updateIn.d.ts.map b/esm/updateIn.d.ts.map new file mode 100644 index 0000000..488ee8e --- /dev/null +++ b/esm/updateIn.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.d.ts","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":"AA4BA,UAAU,QAAQ;IAChB,CAAC,MAAM,KAAA,EAAE,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,GAAG,CAAC;IAC3B,CAAC,IAAI,KAAA,EAAE,KAAK,KAAA,GAAG,CAAC,MAAM,KAAA,KAAK,GAAG,CAAC;CAChC;;AAED,wBAA0C"} \ No newline at end of file diff --git a/esm/updateIn.js b/esm/updateIn.js new file mode 100644 index 0000000..4e98868 --- /dev/null +++ b/esm/updateIn.js @@ -0,0 +1,21 @@ +import update from "./update.js"; +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; +import map from "./map.js"; +const wildcard = "*"; +function reducePath(acc, key) { + if (key === wildcard) { + return (value) => Object.prototype.hasOwnProperty.call(value, wildcard) + ? // If we actually have wildcard as a property, update that + update(value, { [wildcard]: acc }) + : // Otherwise map over all properties + map(value, acc); + } + return { [key]: acc }; +} +function updateIn(object, path, value) { + const updates = splitPath(path).reduceRight(reducePath, value); + return update(object, updates); +} +export default wrap(updateIn); +//# sourceMappingURL=updateIn.js.map \ No newline at end of file diff --git a/esm/updateIn.js.map b/esm/updateIn.js.map new file mode 100644 index 0000000..21e4cc2 --- /dev/null +++ b/esm/updateIn.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.js","sourceRoot":"","sources":["../src/updateIn.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,GAAG,MAAM,UAAU,CAAC;AAE3B,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB,SAAS,UAAU,CAAC,GAAG,EAAE,GAAG;IAC1B,IAAI,GAAG,KAAK,QAAQ,EAAE;QACpB,OAAO,CAAC,KAAK,EAAE,EAAE,CACf,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC;YACnD,CAAC,CAAC,0DAA0D;gBAC1D,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC;YACpC,CAAC,CAAC,oCAAoC;gBACpC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KACvB;IAED,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK;IACnC,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAE/D,OAAO,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC;AAOD,eAAe,IAAI,CAAC,QAAQ,CAAa,CAAC"} \ No newline at end of file diff --git a/esm/updateIn.test.d.ts b/esm/updateIn.test.d.ts new file mode 100644 index 0000000..6a23d85 --- /dev/null +++ b/esm/updateIn.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updateIn.test.d.ts.map \ No newline at end of file diff --git a/esm/updateIn.test.d.ts.map b/esm/updateIn.test.d.ts.map new file mode 100644 index 0000000..6be947e --- /dev/null +++ b/esm/updateIn.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updateIn.test.d.ts","sourceRoot":"","sources":["../src/updateIn.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/updateIn.test.js b/esm/updateIn.test.js new file mode 100644 index 0000000..b376a79 --- /dev/null +++ b/esm/updateIn.test.js @@ -0,0 +1,52 @@ +import { expect, test, describe } from "vitest"; +import updateIn from "./updateIn.js"; +describe("updateIn", () => { + test("can update a single path described with a string", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("can update a single path described with a string with a function", () => { + const inc = (x) => x + 1; + const object = { a: { b: 0 } }; + const result = updateIn("a.b", inc)(object); + expect(result).to.eql({ a: { b: 1 } }); + }); + test("can update a single path described with an array", () => { + const object = { a: { b: 0 } }; + const result = updateIn(["a", "b"], 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("can update arrays", () => { + const object = { a: [0, 0, 0] }; + const result = updateIn("a.1", 3)(object); + expect(result).to.eql({ a: [0, 3, 0] }); + }); + test("can be partially applied", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + test("replaces the object outright if the path is empty", () => { + const object = {}; + const result = updateIn("", 3)(object); + expect(result).to.equal(3); + }); + test("freezes the result", () => { + expect(Object.isFrozen(updateIn("a", 0)({}))).to.be.true; + }); + test("can multiple elements of an array with *", () => { + let object = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] }; + let result = updateIn("a.*.b", (x) => x + 1)(object); + expect(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] }); + object = { a: [0, 1, 2] }; + result = updateIn(["a", "*"], (x) => x + 1)(object); + expect(result).to.eql({ a: [1, 2, 3] }); + }); + test("can update properties named *", () => { + const object = { "*": 1, x: 1 }; + const result = updateIn("*", (x) => x + 1)(object); + expect(result).to.eql({ "*": 2, x: 1 }); + }); +}); +//# sourceMappingURL=updateIn.test.js.map \ No newline at end of file diff --git a/esm/updateIn.test.js.map b/esm/updateIn.test.js.map new file mode 100644 index 0000000..e138999 --- /dev/null +++ b/esm/updateIn.test.js.map @@ -0,0 +1 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M,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/updeep.bench.d.ts b/esm/updeep.bench.d.ts new file mode 100644 index 0000000..87237b2 --- /dev/null +++ b/esm/updeep.bench.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.bench.d.ts.map \ No newline at end of file diff --git a/esm/updeep.bench.d.ts.map b/esm/updeep.bench.d.ts.map new file mode 100644 index 0000000..393d6cf --- /dev/null +++ b/esm/updeep.bench.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.d.ts","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/updeep.bench.js b/esm/updeep.bench.js new file mode 100644 index 0000000..bcdfd75 --- /dev/null +++ b/esm/updeep.bench.js @@ -0,0 +1,10 @@ +import original from "updeep"; +import contender from "./index.js"; +import { bench } from "vitest"; +bench("original, simple update", () => { + original({ a: 1 })({ a: 2, b: 3 }); +}); +bench("contender, simple update", () => { + contender({ a: 1 })({ a: 2, b: 3 }); +}); +//# sourceMappingURL=updeep.bench.js.map \ No newline at end of file diff --git a/esm/updeep.bench.js.map b/esm/updeep.bench.js.map new file mode 100644 index 0000000..06af885 --- /dev/null +++ b/esm/updeep.bench.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.bench.js","sourceRoot":"","sources":["../src/updeep.bench.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,QAAQ,CAAC;AAC9B,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,KAAK,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACnC,QAAgB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,KAAK,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACrC,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/updeep.test.d.ts b/esm/updeep.test.d.ts new file mode 100644 index 0000000..c9a3e6b --- /dev/null +++ b/esm/updeep.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=updeep.test.d.ts.map \ No newline at end of file diff --git a/esm/updeep.test.d.ts.map b/esm/updeep.test.d.ts.map new file mode 100644 index 0000000..96f1294 --- /dev/null +++ b/esm/updeep.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.d.ts","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/updeep.test.js b/esm/updeep.test.js new file mode 100644 index 0000000..5622b61 --- /dev/null +++ b/esm/updeep.test.js @@ -0,0 +1,108 @@ +import { expect, it, describe } from "vitest"; +import u from "./index.js"; +it("does not change anything if no updates are specified", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, {}); + expect(result).to.equal(object); +}); +it("can update with fixed values", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, { foo: 4 }); + expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); +}); +it("returns the same instance if an update doesn't make changes", () => { + const object = { foo: 3 }; + const result = u({ foo: 3 })(object); + expect(result).to.equal(object); +}); +it("can update a nested structure", () => { + const object = { foo: { bar: 7, bam: 3 }, baz: 32 }; + const result = u({ foo: { bar: 8 } })(object); + expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); +}); +it("can update arrays", () => { + const object = [1, 2, 3]; + const result = u({ 1: 7 })(object); + expect(result).to.deep.equal([1, 7, 3]); +}); +it("replaces the object outright if updates are a constant", () => { + expect(u(3)({})).to.equal(3); + expect(u(null)({})).to.be.null; +}); +it("can add an element to an array", () => { + const object = []; + const result = u({ 0: 3 })(object); + expect(result).to.eql([3]); +}); +it("can update nested arrays", () => { + const object = { foo: [1, 2, 3], bar: 9 }; + const result = u({ foo: { 1: 7 } })(object); + expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); +}); +it("can use functions to update values", () => { + const inc = (i) => i + 1; + const object = { foo: 3, bar: 4, baz: 7 }; + const result = u({ foo: inc, bar: inc })(object); + expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); +}); +it("can be partially applied", () => { + const inc = (i) => i + 1; + const object = { foo: 3 }; + const incFoo = u({ foo: inc }); + const result = incFoo(object); + expect(result).to.deep.equal({ foo: 4 }); +}); +it("can update if the value is an array", () => { + const object = {}; + const result = u({ foo: [0, 1] })(object); + expect(result).to.deep.equal({ foo: [0, 1] }); +}); +it("can update when original object is undefined", () => { + const result = u({ foo: [0, 1] })(undefined); + expect(result).to.deep.equal({ foo: [0, 1] }); +}); +it("can take a function as the updater", () => { + const result = u((i) => i + 1)(7); + expect(result).to.eql(8); +}); +it("deeply freezes the result", () => { + const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }); + expect(Object.isFrozen(result)).to.be.true; + expect(Object.isFrozen(result.foo)).to.be.true; +}); +it("assigns null values", () => { + expect(u({ isNull: null }, {})).to.eql({ isNull: null }); +}); +it("defaults to an empty object when null or undefined", () => { + let result = u({ a: { b: 0 } })({ a: null }); + expect(result).to.eql({ a: { b: 0 } }); + result = u({ a: { b: 0 } })({ a: undefined }); + expect(result).to.eql({ a: { b: 0 } }); + result = u({ a: { b: 0 } })({}); + expect(result).to.eql({ a: { b: 0 } }); +}); +it("preserves empty objects when empty updates are specified", () => { + const result = u({ a: {} })({}); + expect(result).to.eql({ a: {} }); +}); +it("works with date objects", () => { + const date = new Date(); + const result = u({ created: date })({}); + expect(result).toEqual({ created: date }); +}); +const expectU = (update, orig, expected) => expect(update(orig)).to.eql(expected); +describe("u.omitted", () => { + it("omit properties via u.omitted", () => { + expectU(u({ a: u.omitted, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }); + }); + it("omit array and object properties", () => { + expectU(u({ a: u.omitted, b: "stuff", c: u.omitted }), { a: [1, 2, 3], b: "orig", c: { z: "bar" } }, { b: "stuff" }); + }); + it("deep omit", () => { + expectU(u({ a: { b: u.omitted, c: "stuff" } }), { a: { b: "foo", z: "bar" } }, { a: { z: "bar", c: "stuff" } }); + }); + it("omitting an array item filters it out", () => { + expectU(u({ 1: u.omitted }), ["a", "b", "c"], ["a", "c"]); + }); +}); +//# sourceMappingURL=updeep.test.js.map \ No newline at end of file diff --git a/esm/updeep.test.js.map b/esm/updeep.test.js.map new file mode 100644 index 0000000..2453ac0 --- /dev/null +++ b/esm/updeep.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"updeep.test.js","sourceRoot":"","sources":["../src/updeep.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,CAAC,MAAM,YAAY,CAAC;AAE3B,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;IAC9D,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IACvC,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CA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;IAEH,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,OAAO,CACL,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAC7C,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAC5C,EAAE,CAAC,EAAE,OAAO,EAAE,CACf,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;QACnB,OAAO,CACL,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC,EACtC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAC7B,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAChC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/util/curry.d.ts b/esm/util/curry.d.ts new file mode 100644 index 0000000..d6daf4a --- /dev/null +++ b/esm/util/curry.d.ts @@ -0,0 +1,7 @@ +export function curry1(fn: any): (a: any, ...args: any[]) => any; +export function curry2(fn: any): (a: any, b: any, ...args: any[]) => any; +export function curry3(fn: any): (a: any, b: any, c: any, ...args: any[]) => any; +export function curry4(fn: any): (a: any, b: any, c: any, d: any, ...args: any[]) => any; +export default function curry(fn: any, length?: any): any; +export const _: "@@updeep/placeholder"; +//# sourceMappingURL=curry.d.ts.map \ No newline at end of file diff --git a/esm/util/curry.d.ts.map b/esm/util/curry.d.ts.map new file mode 100644 index 0000000..06c88c6 --- /dev/null +++ b/esm/util/curry.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"curry.d.ts","sourceRoot":"","sources":["../../src/util/curry.js"],"names":[],"mappings":"AAcA,iEAQC;AAED,yEAmBC;AAED,iFA6BC;AAED,yFA6CC;AAED,0DAEC;AA5HD,uCAAuC"} \ No newline at end of file diff --git a/esm/util/curry.js b/esm/util/curry.js new file mode 100644 index 0000000..82a7856 --- /dev/null +++ b/esm/util/curry.js @@ -0,0 +1,115 @@ +/* eslint no-shadow:0, no-param-reassign:0 prefer-rest-params:0 */ +export const _ = '@@updeep/placeholder'; +function countArguments(args, max) { + let n = args.length; + if (n > max) + n = max; + while (args[n - 1] === _) { + n -= 1; + } + return n; +} +export function curry1(fn) { + return function curried(a, ...args) { + const [b, c] = args; + const n = countArguments(arguments); + if (n >= 1) + return fn(a, b, c); + return curried; + }; +} +export function curry2(fn) { + return function curried(a, b, ...args) { + const [c, d] = args; + const n = countArguments(arguments, 2); + if (b === _ || c === _ || d === _) { + throw new Error('Can only use placeholder on first argument of this function.'); + } + if (n >= 2) { + if (a === _) + return curry1((a, c, d) => fn(a, b, c, d)); + return fn(a, b, c, d); + } + if (n === 1) + return curry1((b, c, d) => fn(a, b, c, d)); + return curried; + }; +} +export function curry3(fn) { + return function curried(a, b, c, ...args) { + const [d, e] = args; + const n = countArguments(arguments, 3); + if (c === _ || d === _ || e === _) { + throw new Error('Can only use placeholder on first or second argument of this function.'); + } + if (n >= 3) { + if (a === _) { + if (b === _) + return curry2((a, b, d, e) => fn(a, b, c, d, e)); + return curry1((a, d, e) => fn(a, b, c, d, e)); + } + if (b === _) + return curry1((b, d, e) => fn(a, b, c, d, e)); + return fn(a, b, c, d, e); + } + if (n === 2) { + if (a === _) + return curry2((a, c, d, e) => fn(a, b, c, d, e)); + return curry1((c, d, e) => fn(a, b, c, d, e)); + } + if (n === 1) + return curry2((b, c, d, e) => fn(a, b, c, d, e)); + return curried; + }; +} +export function curry4(fn) { + return function curried(a, b, c, d, ...args) { + const [e, f] = args; + const n = countArguments(arguments, 4); + if (d === _ || e === _ || f === _) { + throw new Error('Can only use placeholder on first, second or third argument of this function.'); + } + if (n >= 4) { + if (a === _) { + if (b === _) { + if (c === _) + return curry3((a, b, c, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === _) + return curry2((a, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((a, e, f) => fn(a, b, c, d, e, f)); + } + if (b === _) { + if (c === _) + return curry2((b, c, e, f) => fn(a, b, c, d, e, f)); + return curry1((b, e, f) => fn(a, b, c, d, e, f)); + } + if (c === _) + return curry1((c, e, f) => fn(a, b, c, d, e, f)); + return fn(a, b, c, d, e, f); + } + if (n === 3) { + if (a === _) { + if (b === _) + return curry3((a, b, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((a, d, e, f) => fn(a, b, c, d, e, f)); + } + if (b === _) + return curry2((b, d, e, f) => fn(a, b, c, d, e, f)); + return curry1((d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 2) { + if (a === _) + return curry3((a, c, d, e, f) => fn(a, b, c, d, e, f)); + return curry2((c, d, e, f) => fn(a, b, c, d, e, f)); + } + if (n === 1) + return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f)); + return curried; + }; +} +export default function curry(fn, length = fn.length) { + return [fn, curry1, curry2, curry3, curry4][length](fn); +} +//# sourceMappingURL=curry.js.map \ No newline at end of file diff --git a/esm/util/curry.js.map b/esm/util/curry.js.map new file mode 100644 index 0000000..5b3b687 --- /dev/null +++ b/esm/util/curry.js.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line at end of file diff --git a/esm/util/splitPath.d.ts b/esm/util/splitPath.d.ts new file mode 100644 index 0000000..6daeb5e --- /dev/null +++ b/esm/util/splitPath.d.ts @@ -0,0 +1,2 @@ +export default function splitPath(path: string | number | (number | string)[]): (string | number)[]; +//# sourceMappingURL=splitPath.d.ts.map \ No newline at end of file diff --git a/esm/util/splitPath.d.ts.map b/esm/util/splitPath.d.ts.map new file mode 100644 index 0000000..686b455 --- /dev/null +++ b/esm/util/splitPath.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,uBAM5E"} \ No newline at end of file diff --git a/esm/util/splitPath.js b/esm/util/splitPath.js new file mode 100644 index 0000000..b1adbff --- /dev/null +++ b/esm/util/splitPath.js @@ -0,0 +1,8 @@ +export default function splitPath(path) { + if (typeof path === "number") + path = new String(path); + return Array.isArray(path) + ? path + : path.split(".").filter((x) => x !== ""); +} +//# sourceMappingURL=splitPath.js.map \ No newline at end of file diff --git a/esm/util/splitPath.js.map b/esm/util/splitPath.js.map new file mode 100644 index 0000000..65caef2 --- /dev/null +++ b/esm/util/splitPath.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.js","sourceRoot":"","sources":["../../src/util/splitPath.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAA2C;IAC3E,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAQ,CAAC;IAE7D,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACxB,CAAC,CAAC,IAAI;QACN,CAAC,CAAE,IAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;AAC1D,CAAC"} \ No newline at end of file diff --git a/esm/util/splitPath.test.d.ts b/esm/util/splitPath.test.d.ts new file mode 100644 index 0000000..daa51d7 --- /dev/null +++ b/esm/util/splitPath.test.d.ts @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=splitPath.test.d.ts.map \ No newline at end of file diff --git a/esm/util/splitPath.test.d.ts.map b/esm/util/splitPath.test.d.ts.map new file mode 100644 index 0000000..2866cd6 --- /dev/null +++ b/esm/util/splitPath.test.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.d.ts","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/esm/util/splitPath.test.js b/esm/util/splitPath.test.js new file mode 100644 index 0000000..0b53b4e --- /dev/null +++ b/esm/util/splitPath.test.js @@ -0,0 +1,18 @@ +import { expect, it } from "vitest"; +import splitPath from "./splitPath.js"; +it("treats a number as a single step path", () => { + const path = 1; + const result = splitPath(path); + expect(result).to.deep.equal(["1"]); +}); +it("handles arrays", () => { + const path = ["foo", "bar", "x"]; + const result = splitPath(path); + expect(result).to.equal(path); +}); +it("handles strings separated by dots", () => { + const path = "bar.0.y"; + const result = splitPath(path); + expect(result).to.deep.equal(["bar", "0", "y"]); +}); +//# sourceMappingURL=splitPath.test.js.map \ No newline at end of file diff --git a/esm/util/splitPath.test.js.map b/esm/util/splitPath.test.js.map new file mode 100644 index 0000000..f2ae275 --- /dev/null +++ b/esm/util/splitPath.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"splitPath.test.js","sourceRoot":"","sources":["../../src/util/splitPath.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;IAC/C,MAAM,IAAI,GAAG,CAAC,CAAC;IACf,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACtC,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;IACxB,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC3C,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAClD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/esm/wrap.d.ts b/esm/wrap.d.ts new file mode 100644 index 0000000..aa294b7 --- /dev/null +++ b/esm/wrap.d.ts @@ -0,0 +1,3 @@ +declare const wrap: (fn: any) => (...args: any[]) => Readonly; +export default wrap; +//# sourceMappingURL=wrap.d.ts.map \ No newline at end of file diff --git a/esm/wrap.d.ts.map b/esm/wrap.d.ts.map new file mode 100644 index 0000000..5061f46 --- /dev/null +++ b/esm/wrap.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,IAAI,gDAWP,CAAC;AAEJ,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/esm/wrap.js b/esm/wrap.js new file mode 100644 index 0000000..c106306 --- /dev/null +++ b/esm/wrap.js @@ -0,0 +1,13 @@ +import freeze from "./freeze.js"; +const wrap = (fn) => (...args) => { + const diff = fn.length - args.length; + if (diff === 0) { + return freeze(fn(...args)); + } + if (diff === 1) { + return (data) => freeze(fn(data, ...args)); + } + throw new Error("Wrong number of arguments"); +}; +export default wrap; +//# sourceMappingURL=wrap.js.map \ No newline at end of file diff --git a/esm/wrap.js.map b/esm/wrap.js.map new file mode 100644 index 0000000..2248993 --- /dev/null +++ b/esm/wrap.js.map @@ -0,0 +1 @@ +{"version":3,"file":"wrap.js","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,IAAI,GACR,CAAC,EAAE,EAAE,EAAE,CACP,CAAC,GAAG,IAAI,EAAE,EAAE;IACV,MAAM,IAAI,GAAG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACrC,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5B;IACD,IAAI,IAAI,KAAK,CAAC,EAAE;QACd,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;KAC5C;IACD,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;AAC/C,CAAC,CAAC;AAEJ,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/husky.config.js b/husky.config.js deleted file mode 100644 index eba21a7..0000000 --- a/husky.config.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -module.exports = { - hooks: { - "pre-commit": "yarn run lint-staged", - }, -} diff --git a/lib/freeze.js b/lib/freeze.js deleted file mode 100644 index 918d43e..0000000 --- a/lib/freeze.js +++ /dev/null @@ -1,62 +0,0 @@ -function isFreezable(object) { - if (object === null) return false - if (object instanceof RegExp) return false - if (object instanceof ArrayBuffer) return false - - return Array.isArray(object) || typeof object === 'object' -} - -function needsFreezing(object) { - return isFreezable(object) && !Object.isFrozen(object) -} - -function recur(object) { - Object.freeze(object) - - Object.keys(object).forEach((key) => { - const value = object[key] - if (needsFreezing(value)) { - recur(value) - } - }) - - return object -} - -/** - * Deeply freeze a plain javascript object. - * - * If `process.env.NODE_ENV === 'production'`, this returns the original object - * without freezing. - * - * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object - * without freezing. - * - * If in a browser and built without replacing process, never freeze. - * - * @function - * @sig a -> a - * @param {object} object Object to freeze. - * @return {object} Frozen object, unless in production, then the same object. - */ -function freeze(object) { - if (typeof process === 'undefined') { - return object - } - - if (process.env.NODE_ENV === 'production') { - return object - } - - if (process.env.UPDEEP_MODE === 'dangerously_never_freeze') { - return object - } - - if (needsFreezing(object)) { - recur(object) - } - - return object -} - -export default freeze diff --git a/lib/if.js b/lib/if.js deleted file mode 100644 index e3fcb15..0000000 --- a/lib/if.js +++ /dev/null @@ -1,6 +0,0 @@ -import ifElse from './ifElse' -import curry from './util/curry' - -export default curry((predicate, trueUpdates, object) => - ifElse(predicate, trueUpdates, (x) => x, object) -) diff --git a/lib/ifElse.js b/lib/ifElse.js deleted file mode 100644 index 5e6c107..0000000 --- a/lib/ifElse.js +++ /dev/null @@ -1,12 +0,0 @@ -import update from './update' -import wrap from './wrap' - -function updateIfElse(predicate, trueUpdates, falseUpdates, object) { - const test = typeof predicate === 'function' ? predicate(object) : predicate - - const updates = test ? trueUpdates : falseUpdates - - return update(updates, object) -} - -export default wrap(updateIfElse) diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 9130337..0000000 --- a/lib/index.js +++ /dev/null @@ -1,32 +0,0 @@ -import constant from './constant' -import freeze from './freeze' -import is from './is' -import _if from './if' -import ifElse from './ifElse' -import map from './map' -import omit from './omit' -import omitBy from './omitBy' -import reject from './reject' -import update, { omitted } from './update' -import updateIn from './updateIn' -import withDefault from './withDefault' -import { _ } from './util/curry' - -const u = update - -u._ = _ -u.constant = constant -u.if = _if -u.ifElse = ifElse -u.is = is -u.freeze = freeze -u.map = map -u.omit = omit -u.omitBy = omitBy -u.reject = reject -u.update = update -u.updateIn = updateIn -u.omitted = omitted -u.withDefault = withDefault - -export default u diff --git a/lib/is.js b/lib/is.js deleted file mode 100644 index bde98d5..0000000 --- a/lib/is.js +++ /dev/null @@ -1,21 +0,0 @@ -import splitPath from './util/splitPath' -import curry from './util/curry' - -function is(path, predicate, object) { - const parts = splitPath(path) - - let rest = object - for (let i = 0; i < parts.length; i += 1) { - if (typeof rest === 'undefined') return false - const part = parts[i] - rest = rest[part] - } - - if (typeof predicate === 'function') { - return predicate(rest) - } - - return predicate === rest -} - -export default curry(is) diff --git a/lib/map.js b/lib/map.js deleted file mode 100644 index cb685b1..0000000 --- a/lib/map.js +++ /dev/null @@ -1,33 +0,0 @@ -import forEach from 'lodash/forEach' -import mapArray from 'lodash/map' -import mapObject from 'lodash/mapValues' -import update from './update' -import wrap from './wrap' - -function shallowEqual(object, otherObject) { - let equal = true - // eslint-disable-next-line consistent-return - forEach(otherObject, (value, key) => { - if (value !== object[key]) { - equal = false - - // exit early - return false - } - }) - - return equal -} - -function map(iteratee, object) { - const updater = typeof iteratee === 'function' ? iteratee : update(iteratee) - - const mapper = Array.isArray(object) ? mapArray : mapObject - - const newObject = mapper(object, updater) - const equal = shallowEqual(object, newObject) - - return equal ? object : newObject -} - -export default wrap(map) diff --git a/lib/omit.js b/lib/omit.js deleted file mode 100644 index 93f76d0..0000000 --- a/lib/omit.js +++ /dev/null @@ -1,8 +0,0 @@ -import _omit from 'lodash/omit' -import wrap from './wrap' - -function omit(predicate, collection) { - return _omit(collection, predicate) -} - -export default wrap(omit) diff --git a/lib/omitBy.js b/lib/omitBy.js deleted file mode 100644 index 4eab507..0000000 --- a/lib/omitBy.js +++ /dev/null @@ -1,8 +0,0 @@ -import _omitBy from 'lodash/omitBy' -import wrap from './wrap' - -function omitBy(predicate, collection) { - return _omitBy(collection, predicate) -} - -export default wrap(omitBy) diff --git a/lib/reject.js b/lib/reject.js deleted file mode 100644 index 55046c0..0000000 --- a/lib/reject.js +++ /dev/null @@ -1,11 +0,0 @@ -import _reject from 'lodash/reject' -import wrap from './wrap' - -function reject(predicate, collection) { - const result = _reject(collection, predicate) - const equal = collection.length === result.length - - return equal ? collection : result -} - -export default wrap(reject) diff --git a/lib/updateIn.js b/lib/updateIn.js deleted file mode 100644 index eb44164..0000000 --- a/lib/updateIn.js +++ /dev/null @@ -1,28 +0,0 @@ -import curry from './util/curry' -import update from './update' -import map from './map' -import splitPath from './util/splitPath' - -const wildcard = '*' - -function reducePath(acc, key) { - if (key === wildcard) { - return (value) => - Object.prototype.hasOwnProperty.call(value, wildcard) - ? // If we actually have wildcard as a property, update that - update({ [wildcard]: acc }, value) - : // Otherwise map over all properties - map(acc, value) - } - - return { [key]: acc } -} - -function updateIn(path, value, object) { - const parts = splitPath(path) - const updates = parts.reduceRight(reducePath, value) - - return update(updates, object) -} - -export default curry(updateIn) diff --git a/lib/util/splitPath.js b/lib/util/splitPath.js deleted file mode 100644 index d305306..0000000 --- a/lib/util/splitPath.js +++ /dev/null @@ -1,5 +0,0 @@ -import reject from 'lodash/reject' - -export default function splitPath(path) { - return Array.isArray(path) ? path : reject(`${path}`.split('.'), (x) => !x) -} diff --git a/lib/withDefault.js b/lib/withDefault.js deleted file mode 100644 index 5e0d54a..0000000 --- a/lib/withDefault.js +++ /dev/null @@ -1,12 +0,0 @@ -import update from './update' -import curry from './util/curry' - -function withDefault(defaultValue, updates, object) { - if (typeof object === 'undefined') { - return update(updates, defaultValue) - } - - return update(updates, object) -} - -export default curry(withDefault) diff --git a/lib/wrap.js b/lib/wrap.js deleted file mode 100644 index d28215d..0000000 --- a/lib/wrap.js +++ /dev/null @@ -1,6 +0,0 @@ -import curry from './util/curry' -import freeze from './freeze' - -export default function wrap(func, length = func.length) { - return curry((...args) => freeze(func(...args)), length) -} diff --git a/lint-staged.config.js b/lint-staged.config.js deleted file mode 100644 index b274927..0000000 --- a/lint-staged.config.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict' - -module.exports = { - '*.{js,ts}': ['eslint --fix'], - '*.{md,json,html,yml}': ['prettier --write'], -} diff --git a/package.json b/package.json index d546736..062216e 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "module", "name": "updeep", "version": "1.2.1", "description": "Easily update nested frozen objects and arrays in a declarative and immutable manner.", @@ -18,10 +19,16 @@ "functional", "declarative" ], - "source": "lib/index.js", - "main": "dist/index.js", - "module": "dist/index.module.js", - "types": "types", + "source": "./src/index.ts", + "main": "./commonjs/index.cjs", + "types": "./esm/index.d.ts", + "exports": { + ".": { + "types": "./esm/index.d.ts", + "import": "./esm/index.js", + "require": "./commonjs/index.cjs" + } + }, "license": "MIT", "bugs": { "url": "https://github.com/substantial/updeep/issues" @@ -36,35 +43,23 @@ "test": "mocha --recursive --require \"@babel/register\" test" }, "dependencies": { - "lodash": ">=4.2.0" + "remeda": "^1.3.0", + "updeep": "^1.2.1" }, "devDependencies": { - "@babel/core": "*", - "@babel/plugin-proposal-object-rest-spread": "*", - "@babel/preset-env": "*", - "@babel/preset-typescript": "*", - "@babel/register": "*", - "@types/chai": "*", - "@types/lodash": "*", - "@types/mocha": "*", "@types/node": "*", "@typescript-eslint/eslint-plugin": "*", "@typescript-eslint/parser": "*", + "@vitest/ui": "^0.26.3", "benchmark": "*", - "chai": "*", - "chalk": "*", "dtslint": "*", "eslint": "*", "eslint-config-prettier": "*", "eslint-plugin-expect-type": "*", "eslint-plugin-import": "*", "eslint-plugin-prettier": "*", - "husky": "*", - "lint-staged": "*", - "microbundle": "*", - "mocha": "*", "prettier": "*", - "table": "*", - "typescript": "*" + "typescript": "^4.9.4", + "vitest": "^0.26.3" } } diff --git a/perf/index.js b/perf/index.js deleted file mode 100644 index c88ea50..0000000 --- a/perf/index.js +++ /dev/null @@ -1,104 +0,0 @@ -import Benchmark from 'benchmark' -import {table} from 'table' -import chalk from 'chalk' - -import _ from 'lodash' -import u from '../dist/index.umd.js' -import {curry2, curry4 } from '../lib/util/curry' - -const add4 = (a, b, c, d) => a + b + c + d -const add2 = (a, b) => a + b -const fakeCurryAdd = (x) => (y) => x + y -const lodashCurryAdd2 = _.curry(add2) -const updeepCurryAdd2 = curry2(add2) -const lodashCurryAdd4 = _.curry(add4) -const updeepCurryAdd4 = curry4(add4) -// const updeepCurryBig = curry.curryBig(add); - -const array = [0, 1, 2, 3, 4, 5] -// const doUpdate = u(x => x + 1); - - -const log = console.log - -function createSuite(suiteName, tests) { - const results = [] - - const suite = Benchmark.Suite({ - onCycle: (event) => { - results.push(event.target) - }, - onError: (event) => { - console.error(event) - }, - }) - - _.each(tests, (fn, testName) => { - suite.add(testName, fn) - }) - - return () => new Promise((resolve, reject) => { - suite.on('complete', () => resolve({ - suiteName, - results, - })).on('error', reject).run({ async: true}) - }) -} - -const curryVsLodash = createSuite('Curry', { - 'updeep curry': () => { - updeepCurryAdd4(3)(4)(5)(6) - updeepCurryAdd4(3, 4, 5, 6) - updeepCurryAdd4(u._, 4, u._, 6)(3, 4) - updeepCurryAdd2(3)(4) - updeepCurryAdd2(3, 4) - }, - 'lodash curry': () => { - lodashCurryAdd4(3)(4)(5)(6) - lodashCurryAdd4(3, 4, 5, 6) - lodashCurryAdd4(_, 4, _, 6)(3, 4) - lodashCurryAdd2(3)(4) - lodashCurryAdd2(3, 4) - }, -}) - -const mapVsLodash = createSuite('Map', { - '_.map': () => _.map(array, fakeCurryAdd(8)), - 'u.map': () => u.map(fakeCurryAdd(8), array), -}) - -const fn = (a, b, c, d, e) => a + b + c + d + e -const fnControl = (a, b, c, d, e) => fn(a, b, c, d, e) -const fnApply = (...args) => fn(...args) -const fnDestructure = (...args) => { - const [a, b, c, d, e] = args - return fn(a, b, c, d, e) -} -const applyVsDestructure = createSuite('apply vs destructure', { - control: () => fnControl(1, 2, 3, 4, 5), - apply: () => fnApply(1, 2, 3, 4, 5), - destructure: () => fnDestructure(1, 2, 3, 4, 5), -}) - -const printSuiteResults = (suiteResults) => { - const HEADERS = ['Suite Name', 'Results (fastest first)'].map(s => chalk.bold(s)) - - const data = suiteResults.reduce((acc, {suiteName, results}) => { - const row = [ - chalk.cyan(suiteName), - results.sort((a, b) => -a.compare(b)).map(String).join('\n'), - ] - - acc.push(row) - return acc - }, [HEADERS]) - - log(table(data)) -} - - -Promise.all([ - curryVsLodash(), - mapVsLodash(), - // applyVsDestructure(), -]).then(printSuiteResults) diff --git a/prettier.config.js b/prettier.config.js deleted file mode 100644 index 57c4142..0000000 --- a/prettier.config.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -module.exports = { - endOfLine: 'lf', - semi: false, - singleQuote: true, - tabWidth: 2, - trailingComma: 'es5', - bracketSpacing: false, - - proseWrap: 'always', -} diff --git a/prettier.config.mjs b/prettier.config.mjs new file mode 100644 index 0000000..71bce70 --- /dev/null +++ b/prettier.config.mjs @@ -0,0 +1,10 @@ +export default { + endOfLine: "lf", + semi: true, + singleQuote: false, + tabWidth: 2, + trailingComma: "es5", + bracketSpacing: true, + + proseWrap: "always", +}; diff --git a/src/constant.test.ts b/src/constant.test.ts new file mode 100644 index 0000000..012209e --- /dev/null +++ b/src/constant.test.ts @@ -0,0 +1,15 @@ +import { expect, describe, test } from "vitest"; +import constant from "./constant.js"; + +test("returns what it is given... constantly", () => { + const func: any = constant(4); + + expect(func()).to.equal(4); + expect(func("hi")).to.equal(4); + expect(func("hi", 8)).to.equal(4); + expect(func(4)).to.equal(4); +}); + +test("freezes the result", () => { + expect(Object.isFrozen(constant({})())).toBeTruthy(); +}); diff --git a/lib/constant.js b/src/constant.ts similarity index 84% rename from lib/constant.js rename to src/constant.ts index 285c3b0..7b65df1 100644 --- a/lib/constant.js +++ b/src/constant.ts @@ -1,4 +1,4 @@ -import freeze from './freeze' +import freeze from "./freeze.js"; /** * Returns a function that always returns the supplied value. @@ -32,9 +32,7 @@ import freeze from './freeze' * * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); */ -function constant(value) { - const frozen = freeze(value) - return () => frozen +export default function constant(value: T) { + const frozen = freeze(value); + return () => frozen; } - -export default constant diff --git a/src/freeze.test.ts b/src/freeze.test.ts new file mode 100644 index 0000000..01f6d15 --- /dev/null +++ b/src/freeze.test.ts @@ -0,0 +1,74 @@ +import { describe, afterEach, test, expect } from "vitest"; + +import freeze from "./freeze.js"; + +describe("freeze", () => { + afterEach(() => { + delete process.env.NODE_ENV; + }); + + test("freezes objects", () => { + const object = {}; + freeze(object); + + expect(Object.isFrozen(object)).to.be.true; + }); + + test("freezes nested objects", () => { + const object = { foo: { bar: 3 } }; + freeze(object); + + expect(Object.isFrozen(object.foo)).to.be.true; + }); + + test("freezes nested arrays", () => { + const object = [[0]]; + freeze(object); + + expect(Object.isFrozen(object)).to.be.true; + expect(Object.isFrozen(object[0])).to.be.true; + }); + + test("ignores functions", () => { + const object = { foo: () => 1 }; + freeze(object); + + expect(Object.isFrozen(object.foo)).to.be.false; + }); + + test("ignores regexps", () => { + const object = { foo: /\d/ }; + freeze(object); + + expect(Object.isFrozen(object.foo)).to.be.false; + }); + + test("does not freeze children if the parent is already frozen", () => { + const object = { foo: {} }; + Object.freeze(object); + freeze(object); + + expect(Object.isFrozen(object.foo)).to.be.false; + }); + + test("does not freeze in production", () => { + process.env.NODE_ENV = "production"; + + const object = {}; + freeze(object); + + expect(Object.isFrozen(object)).to.be.false; + }); + + test("handles null objects", () => { + const object = { foo: null }; + freeze(object); + expect(Object.isFrozen(object)).to.be.true; + }); + + test("returns the same object", () => { + const object = {}; + const result = freeze(object); + expect(result).to.equal(object); + }); +}); diff --git a/src/freeze.ts b/src/freeze.ts new file mode 100644 index 0000000..b81ed8d --- /dev/null +++ b/src/freeze.ts @@ -0,0 +1,62 @@ +function isFreezable(object: unknown) { + if (object === null) return false; + if (object instanceof RegExp) return false; + if (object instanceof ArrayBuffer) return false; + + return Array.isArray(object) || typeof object === "object"; +} + +function needsFreezing(object: unknown) { + return isFreezable(object) && !Object.isFrozen(object); +} + +function recur(object) { + Object.freeze(object); + + Object.keys(object).forEach((key) => { + const value = object[key]; + if (needsFreezing(value)) { + recur(value); + } + }); + + return object; +} + +/** + * Deeply freeze a plain javascript object. + * + * If `process.env.NODE_ENV === 'production'`, this returns the original object + * without freezing. + * + * Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object + * without freezing. + * + * If in a browser and built without replacing process, never freeze. + * + * @function + * @sig a -> a + * @param object Object to freeze. + * @return Frozen object, unless in production, then the same object. + */ +function freeze(object: T): Readonly { + if (typeof process === "undefined") { + return object; + } + + if (process.env.NODE_ENV === "production") { + return object; + } + + if (process.env.UPDEEP_MODE === "dangerously_never_freeze") { + return object; + } + + if (needsFreezing(object)) { + recur(object); + } + + return object; +} + +export default freeze; diff --git a/src/if.test.ts b/src/if.test.ts new file mode 100644 index 0000000..adc0df6 --- /dev/null +++ b/src/if.test.ts @@ -0,0 +1,33 @@ +import { describe, test, expect } from "vitest"; + +import _if from "./if.js"; + +describe("if", () => { + test("does not update if the predicate is false", () => { + const object = { a: 0 }; + let result = _if(false, { b: 1 }, object); + expect(result).to.eql(object); + + result = _if(0, false, 1); + expect(result).to.eql(0); + }); + + test("does update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = _if(aIsThree, { b: 1 }, object); + + expect(result).to.eql({ a: 0 }); + }); + + test("freezes the result", () => { + expect(Object.isFrozen(_if(true, {})({}))).to.be.true; + expect(Object.isFrozen(_if(false, {})({}))).to.be.true; + }); +}); diff --git a/src/if.ts b/src/if.ts new file mode 100644 index 0000000..bff68e8 --- /dev/null +++ b/src/if.ts @@ -0,0 +1,15 @@ +import { identity } from "remeda"; + +import ifElse from "./ifElse.js"; +import wrap from "./wrap.js"; + +interface If { + (object, predicate, trueUpdates): unknown; + (predicate, trueUpdates): (unknown) => unknown; +} + +function _if(object, predicate, trueUpdates) { + return ifElse(object, predicate, trueUpdates, identity); +} + +export default wrap(_if) as If; diff --git a/src/ifElse.test.ts b/src/ifElse.test.ts new file mode 100644 index 0000000..7d7ece4 --- /dev/null +++ b/src/ifElse.test.ts @@ -0,0 +1,45 @@ +import { expect, test, describe } from "vitest"; + +import ifElse from "./ifElse.js"; +import _if from "./if.js"; + +describe("ifElse", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = ifElse(false, { b: 1 }, { b: 2 })(object); + expect(result).to.eql({ a: 0, b: 2 }); + }); + + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = ifElse(true, { b: 1 }, { b: 4 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); + + test("will use the result of a function passed as a predicate", () => { + const object = { a: 0 }; + const aIsThree = (x) => x.a === 3; + const result = ifElse(aIsThree, { b: 1 }, { b: 4 })(object); + + expect(result).to.eql({ a: 0, b: 4 }); + }); + + test("freezes the result", () => { + expect(Object.isFrozen(ifElse(true, {}, {})({}))).to.be.true; + expect(Object.isFrozen(ifElse(false, {}, {})({}))).to.be.true; + }); +}); + +describe("if", () => { + test("does updates with the else if the predicate is false", () => { + const object = { a: 0 }; + const result = _if(false, { b: 1 })(object); + expect(result).not.toHaveProperty("b"); + }); + + test("updates with the true update if the predicate is true", () => { + const object = { a: 0 }; + const result = _if(true, { b: 1 })(object); + expect(result).to.eql({ a: 0, b: 1 }); + }); +}); diff --git a/src/ifElse.ts b/src/ifElse.ts new file mode 100644 index 0000000..09b7d61 --- /dev/null +++ b/src/ifElse.ts @@ -0,0 +1,19 @@ +import update from "./update.js"; +import wrap from "./wrap.js"; + +function updateIfElse(object, predicate, trueUpdates, falseUpdates) { + const test = typeof predicate === "function" ? predicate(object) : predicate; + + const updates = test ? trueUpdates : falseUpdates; + + return update(object, updates); +} + +type Predicate = ((source: any) => boolean) | boolean; + +interface IfElse { + (object, predicate: Predicate, trueUpdates, falseUpdates): unknown; + (predicate: Predicate, trueUpdates, falseUpdates): (unknown) => unknown; +} + +export default wrap(updateIfElse) as IfElse; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..b02655d --- /dev/null +++ b/src/index.ts @@ -0,0 +1,20 @@ +import freeze from "./freeze.js"; +import is from "./is.js"; +import _if from "./if.js"; +import ifElse from "./ifElse.js"; +import update, { omitted } from "./update.js"; +import updateIn from "./updateIn.js"; +import constant from "./constant.js"; + +const u: any = update; + +u.constant = constant; +u.if = _if; +u.ifElse = ifElse; +u.is = is; +u.freeze = freeze; +u.update = update; +u.updateIn = updateIn; +u.omitted = omitted; + +export default u; diff --git a/src/is.test.ts b/src/is.test.ts new file mode 100644 index 0000000..42f87be --- /dev/null +++ b/src/is.test.ts @@ -0,0 +1,58 @@ +import { expect, test } from "vitest"; +import is from "./is.js"; + +test("returns true if path matches a value predicate", () => { + const result = is({ a: { b: 4 } }, "a.b", 4); + + expect(result).to.be.true; +}); + +test("returns true if path matches a function predicate", () => { + const isEven = (x) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 6 } }); + expect(result).to.be.true; +}); + +test("returns false if path matches a value predicate", () => { + const result = is("a.b", 4)({ a: { b: 5 } }); + expect(result).to.be.false; +}); + +test("returns false if path matches a function predicate", () => { + const isEven = (x: number) => x % 2 === 0; + const result = is("a.b", isEven)({ a: { b: 7 } }); + expect(result).to.be.false; +}); + +test("returns false if the path does not exist", () => { + const result = is("a.b.c.d", 4)({ a: { b: {} } }); + expect(result).to.be.false; +}); + +test("can test for undefined", () => { + const result = is("a.b.c", undefined)({ a: { b: {} } }); + expect(result).to.be.true; +}); + +test("tests the actual object if a blank path is given", () => { + const result = is(4, "", 4); + expect(result).toBeTruthy(); +}); + +test("can use arrays as paths", () => { + const result = is(["a", "b"], 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); + +test("can include array indexes in paths", () => { + let result = is("a.1.b", 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; + + result = is(["a", 1, "b"], 4)({ a: [{}, { b: 4 }] }); + expect(result).to.be.true; +}); + +test("can be partially applied", () => { + const result = is("a.b", 4)({ a: { b: 4 } }); + expect(result).to.be.true; +}); diff --git a/src/is.ts b/src/is.ts new file mode 100644 index 0000000..4dacb40 --- /dev/null +++ b/src/is.ts @@ -0,0 +1,28 @@ +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; + +function _is( + object, + path: number | string | (number | string)[], + predicate +): boolean { + const parts = splitPath(path); + + for (const part of parts) { + if (typeof object === "undefined") return false; + object = object[part]; + } + + if (typeof predicate === "function") { + return predicate(object); + } + + return predicate === object; +} + +interface Is { + (object, path: string | number | (number | string)[], predicate): boolean; + (path: string | number | (number | string)[], predicate): (object) => boolean; +} + +export default wrap(_is) as Is; diff --git a/src/map.test.ts b/src/map.test.ts new file mode 100644 index 0000000..687065c --- /dev/null +++ b/src/map.test.ts @@ -0,0 +1,58 @@ +import { expect, test, describe } from "vitest"; +import map from "./map.js"; + +describe("map", () => { + test("applies updates to each item in an array", () => { + const object = [0, 1, 2]; + const inc = (x) => x + 1; + const result = map(object, inc); + + expect(result).to.eql([1, 2, 3]); + }); + + test("applies updates to each value in an object", () => { + const object = { a: 0, b: 1, c: 2 }; + const inc = (x) => x + 1; + const result = map(inc)(object); + + expect(result).to.eql({ a: 1, b: 2, c: 3 }); + }); + + test("can update with a regular updates object", () => { + const object = [{ a: 0 }, { a: 0 }]; + const result = map({ a: 1 })(object); + + expect(result).to.eql([{ a: 1 }, { a: 1 }]); + }); + + test("returns the same object if no updates are made", () => { + const array = [0, 1]; + const ident = (x) => x; + let result = map(ident)(array); + + expect(result).to.equal(array); + + const object = { a: 0 }; + result = map(ident)(object); + + expect(result).to.equal(object); + }); + + test("passes the key or index as the second parameter to the iteratee", () => { + const object = { + a: { x: 0 }, + b: [3, 3], + }; + const setToKey = (_, key) => key; + const result = map(object, map(setToKey)); + + expect(result).to.eql({ + a: { x: "x" }, + b: [0, 1], + }); + }); + + test("freezes the result", () => { + expect(Object.isFrozen(map({}, {}))).to.be.true; + }); +}); diff --git a/src/map.ts b/src/map.ts new file mode 100644 index 0000000..cd42d29 --- /dev/null +++ b/src/map.ts @@ -0,0 +1,29 @@ +import { mapValues, map as _map } from "remeda"; + +import update from "./update.js"; +import wrap from "./wrap.js"; + +function shallowEqual(object, otherObject) { + for (const k in otherObject) { + if (otherObject[k] !== object[k]) return false; + } + return true; +} + +function map(object, iteratee) { + const updater = typeof iteratee === "function" ? iteratee : update(iteratee); + + const mapper = Array.isArray(object) ? _map.indexed : mapValues; + + const newObject = mapper(object, updater); + const equal = shallowEqual(object, newObject); + + return equal ? object : newObject; +} + +interface Map { + (object, iteratee): any; + (iteratee): (object) => any; +} + +export default wrap(map) as Map; diff --git a/lib/update.js b/src/update.ts similarity index 55% rename from lib/update.js rename to src/update.ts index 58eb584..ffaea1e 100644 --- a/lib/update.js +++ b/src/update.ts @@ -1,59 +1,60 @@ -import isPlainObject from 'lodash/isPlainObject' -import _omitBy from 'lodash/omitBy' +import wrap from "./wrap.js"; +import constant from "./constant.js"; -import wrap from './wrap' -import constant from './constant' +import { omitBy, isObject } from "remeda"; -const innerOmitted = { __omitted: true } -export const omitted = constant(innerOmitted) +const innerOmitted = { __omitted: true }; +export const omitted = constant(innerOmitted); function isEmpty(object) { - return !Object.keys(object).length + return !Object.keys(object).length; } function reduce(object, callback, initialValue) { return Object.keys(object).reduce( (acc, key) => callback(acc, object[key], key), initialValue - ) + ); } function resolveUpdates(updates, object) { return reduce( updates, (acc, value, key) => { - let updatedValue = value + let updatedValue = value; if ( !Array.isArray(value) && value !== null && - typeof value === 'object' + typeof value === "object" ) { - updatedValue = update(value, object[key]) // eslint-disable-line no-use-before-define - } else if (typeof value === 'function') { - updatedValue = value(object[key]) + updatedValue = update(object[key], value); // eslint-disable-line no-use-before-define + } else if (typeof value === "function") { + updatedValue = value(object[key]); } if (object[key] !== updatedValue) { - acc[key] = updatedValue // eslint-disable-line no-param-reassign + acc[key] = updatedValue; // eslint-disable-line no-param-reassign } - return acc + return acc; }, {} - ) + ); } function updateArray(updates, object) { - const newArray = [...object] + const newArray = [...object]; Object.keys(updates).forEach((key) => { - newArray[key] = updates[key] - }) + newArray[key] = updates[key]; + }); - return newArray + return newArray; } +const isPlainObject = (value) => value?.constructor === Object; + /** * Recursively update an object or array. * @@ -71,34 +72,39 @@ function updateArray(updates, object) { * @param {Object|Array} object to update * @return {Object|Array} new object with modifications */ -function update(updates, object, ...args) { - if (typeof updates === 'function') { - return updates(object, ...args) +function update(object, updates) { + if (typeof updates === "function") { + return updates(object); } if (!isPlainObject(updates)) { - return updates + return updates; } const defaultedObject = - typeof object === 'undefined' || object === null ? {} : object + typeof object === "undefined" || object === null ? {} : object; - const resolvedUpdates = resolveUpdates(updates, defaultedObject) + const resolvedUpdates = resolveUpdates(updates, defaultedObject); if (isEmpty(resolvedUpdates)) { - return defaultedObject + return defaultedObject; } if (Array.isArray(defaultedObject)) { return updateArray(resolvedUpdates, defaultedObject).filter( (value) => value !== innerOmitted - ) + ); } - return _omitBy( + return omitBy( { ...defaultedObject, ...resolvedUpdates }, (value) => value === innerOmitted - ) + ); } -export default wrap(update, 2) +interface Update { + (object, func): any; + (func): (object) => any; +} + +export default wrap(update) as Update; diff --git a/src/updateIn.test.ts b/src/updateIn.test.ts new file mode 100644 index 0000000..74ffd20 --- /dev/null +++ b/src/updateIn.test.ts @@ -0,0 +1,60 @@ +import { expect, test, describe } from "vitest"; +import updateIn from "./updateIn.js"; + +describe("updateIn", () => { + test("can update a single path described with a string", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + + test("can update a single path described with a string with a function", () => { + const inc = (x) => x + 1; + const object = { a: { b: 0 } }; + const result = updateIn("a.b", inc)(object); + expect(result).to.eql({ a: { b: 1 } }); + }); + + test("can update a single path described with an array", () => { + const object = { a: { b: 0 } }; + const result = updateIn(["a", "b"], 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + + test("can update arrays", () => { + const object = { a: [0, 0, 0] }; + const result = updateIn("a.1", 3)(object); + expect(result).to.eql({ a: [0, 3, 0] }); + }); + + test("can be partially applied", () => { + const object = { a: { b: 0 } }; + const result = updateIn("a.b", 3)(object); + expect(result).to.eql({ a: { b: 3 } }); + }); + + test("replaces the object outright if the path is empty", () => { + const object = {}; + const result = updateIn("", 3)(object); + expect(result).to.equal(3); + }); + test("freezes the result", () => { + expect(Object.isFrozen(updateIn("a", 0)({}))).to.be.true; + }); + + test("can multiple elements of an array with *", () => { + let object: any = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] }; + let result = updateIn("a.*.b", (x) => x + 1)(object); + expect(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] }); + + object = { a: [0, 1, 2] }; + result = updateIn(["a", "*"], (x) => x + 1)(object); + expect(result).to.eql({ a: [1, 2, 3] }); + }); + + test("can update properties named *", () => { + const object = { "*": 1, x: 1 }; + const result = updateIn("*", (x) => x + 1)(object); + expect(result).to.eql({ "*": 2, x: 1 }); + }); +}); diff --git a/src/updateIn.ts b/src/updateIn.ts new file mode 100644 index 0000000..c416bbf --- /dev/null +++ b/src/updateIn.ts @@ -0,0 +1,34 @@ +import { mapValues } from "remeda"; + +import update from "./update.js"; +import splitPath from "./util/splitPath.js"; +import wrap from "./wrap.js"; +import map from "./map.js"; + +const wildcard = "*"; + +function reducePath(acc, key) { + if (key === wildcard) { + return (value) => + Object.prototype.hasOwnProperty.call(value, wildcard) + ? // If we actually have wildcard as a property, update that + update(value, { [wildcard]: acc }) + : // Otherwise map over all properties + map(value, acc); + } + + return { [key]: acc }; +} + +function updateIn(object, path, value) { + const updates = splitPath(path).reduceRight(reducePath, value); + + return update(object, updates); +} + +interface UpdateIn { + (object, path, value): any; + (path, value): (object) => any; +} + +export default wrap(updateIn) as UpdateIn; diff --git a/src/updeep.bench.ts b/src/updeep.bench.ts new file mode 100644 index 0000000..65cd701 --- /dev/null +++ b/src/updeep.bench.ts @@ -0,0 +1,12 @@ +import original from "updeep"; +import contender from "./index.js"; + +import { bench } from "vitest"; + +bench("original, simple update", () => { + (original as any)({ a: 1 })({ a: 2, b: 3 }); +}); + +bench("contender, simple update", () => { + contender({ a: 1 })({ a: 2, b: 3 }); +}); diff --git a/src/updeep.test.ts b/src/updeep.test.ts new file mode 100644 index 0000000..1f3330b --- /dev/null +++ b/src/updeep.test.ts @@ -0,0 +1,155 @@ +import { expect, it, describe } from "vitest"; +import u from "./index.js"; + +it("does not change anything if no updates are specified", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, {}); + + expect(result).to.equal(object); +}); + +it("can update with fixed values", () => { + const object = { foo: 3, bar: [7, 5] }; + const result = u(object, { foo: 4 }); + + expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); +}); + +it("returns the same instance if an update doesn't make changes", () => { + const object = { foo: 3 }; + const result = u({ foo: 3 })(object); + + expect(result).to.equal(object); +}); + +it("can update a nested structure", () => { + const object = { foo: { bar: 7, bam: 3 }, baz: 32 }; + const result = u({ foo: { bar: 8 } })(object); + + expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); +}); + +it("can update arrays", () => { + const object = [1, 2, 3]; + const result = u({ 1: 7 })(object); + + expect(result).to.deep.equal([1, 7, 3]); +}); + +it("replaces the object outright if updates are a constant", () => { + expect(u(3)({})).to.equal(3); + expect(u(null)({})).to.be.null; +}); + +it("can add an element to an array", () => { + const object = []; + const result = u({ 0: 3 })(object); + + expect(result).to.eql([3]); +}); + +it("can update nested arrays", () => { + const object = { foo: [1, 2, 3], bar: 9 }; + const result = u({ foo: { 1: 7 } })(object); + + expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); +}); + +it("can use functions to update values", () => { + const inc = (i) => i + 1; + const object = { foo: 3, bar: 4, baz: 7 }; + const result = u({ foo: inc, bar: inc })(object); + + expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); +}); + +it("can be partially applied", () => { + const inc = (i) => i + 1; + const object = { foo: 3 }; + const incFoo = u({ foo: inc }); + + const result = incFoo(object); + + expect(result).to.deep.equal({ foo: 4 }); +}); + +it("can update if the value is an array", () => { + const object = {}; + const result = u({ foo: [0, 1] })(object); + + expect(result).to.deep.equal({ foo: [0, 1] }); +}); + +it("can update when original object is undefined", () => { + const result = u({ foo: [0, 1] })(undefined); + + expect(result).to.deep.equal({ foo: [0, 1] }); +}); + +it("can take a function as the updater", () => { + const result = u((i) => i + 1)(7); + + expect(result).to.eql(8); +}); + +it("deeply freezes the result", () => { + const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }); + + expect(Object.isFrozen(result)).to.be.true; + expect(Object.isFrozen(result.foo)).to.be.true; +}); + +it("assigns null values", () => { + expect(u({ isNull: null }, {})).to.eql({ isNull: null }); +}); + +it("defaults to an empty object when null or undefined", () => { + let result = u({ a: { b: 0 } })({ a: null }); + expect(result).to.eql({ a: { b: 0 } }); + + result = u({ a: { b: 0 } })({ a: undefined }); + expect(result).to.eql({ a: { b: 0 } }); + + result = u({ a: { b: 0 } })({}); + expect(result).to.eql({ a: { b: 0 } }); +}); + +it("preserves empty objects when empty updates are specified", () => { + const result = u({ a: {} })({}); + expect(result).to.eql({ a: {} }); +}); + +it("works with date objects", () => { + const date = new Date(); + const result = u({ created: date })({}); + expect(result).toEqual({ created: date }); +}); + +const expectU = (update, orig, expected) => + expect(update(orig)).to.eql(expected); + +describe("u.omitted", () => { + it("omit properties via u.omitted", () => { + expectU(u({ a: u.omitted, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }); + }); + + it("omit array and object properties", () => { + expectU( + u({ a: u.omitted, b: "stuff", c: u.omitted }), + { a: [1, 2, 3], b: "orig", c: { z: "bar" } }, + { b: "stuff" } + ); + }); + + it("deep omit", () => { + expectU( + u({ a: { b: u.omitted, c: "stuff" } }), + { a: { b: "foo", z: "bar" } }, + { a: { z: "bar", c: "stuff" } } + ); + }); + + it("omitting an array item filters it out", () => { + expectU(u({ 1: u.omitted }), ["a", "b", "c"], ["a", "c"]); + }); +}); diff --git a/lib/util/curry.js b/src/util/curry.js similarity index 100% rename from lib/util/curry.js rename to src/util/curry.js diff --git a/src/util/splitPath.test.ts b/src/util/splitPath.test.ts new file mode 100644 index 0000000..ee671c4 --- /dev/null +++ b/src/util/splitPath.test.ts @@ -0,0 +1,20 @@ +import { expect, it } from "vitest"; +import splitPath from "./splitPath.js"; + +it("treats a number as a single step path", () => { + const path = 1; + const result = splitPath(path); + expect(result).to.deep.equal(["1"]); +}); + +it("handles arrays", () => { + const path = ["foo", "bar", "x"]; + const result = splitPath(path); + expect(result).to.equal(path); +}); + +it("handles strings separated by dots", () => { + const path = "bar.0.y"; + const result = splitPath(path); + expect(result).to.deep.equal(["bar", "0", "y"]); +}); diff --git a/src/util/splitPath.ts b/src/util/splitPath.ts new file mode 100644 index 0000000..8783e73 --- /dev/null +++ b/src/util/splitPath.ts @@ -0,0 +1,7 @@ +export default function splitPath(path: string | number | (number | string)[]) { + if (typeof path === "number") path = new String(path) as any; + + return Array.isArray(path) + ? path + : (path as string).split(".").filter((x) => x !== ""); +} diff --git a/src/wrap.ts b/src/wrap.ts new file mode 100644 index 0000000..988ee61 --- /dev/null +++ b/src/wrap.ts @@ -0,0 +1,17 @@ +import freeze from "./freeze.js"; +import { purry } from "remeda"; + +const wrap = + (fn) => + (...args) => { + const diff = fn.length - args.length; + if (diff === 0) { + return freeze(fn(...args)); + } + if (diff === 1) { + return (data) => freeze(fn(data, ...args)); + } + throw new Error("Wrong number of arguments"); + }; + +export default wrap; diff --git a/test/constant-spec.js b/test/constant-spec.js deleted file mode 100644 index 83f7c65..0000000 --- a/test/constant-spec.js +++ /dev/null @@ -1,17 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.constant', () => { - it('returns what it is given... constantly', () => { - const func = u.constant(4) - - expect(func()).to.equal(4) - expect(func('hi')).to.equal(4) - expect(func('hi', 8)).to.equal(4) - expect(func(4)).to.equal(4) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.constant({})())).to.be.true - }) -}) diff --git a/test/freeze-spec.js b/test/freeze-spec.js deleted file mode 100644 index 652fed4..0000000 --- a/test/freeze-spec.js +++ /dev/null @@ -1,73 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.freeze', () => { - afterEach(() => { - delete process.env.NODE_ENV - }) - - it('freezes objects', () => { - const object = {} - u.freeze(object) - - expect(Object.isFrozen(object)).to.be.true - }) - - it('freezes nested objects', () => { - const object = { foo: { bar: 3 } } - u.freeze(object) - - expect(Object.isFrozen(object.foo)).to.be.true - }) - - it('freezes nested arrays', () => { - const object = [[0]] - u.freeze(object) - - expect(Object.isFrozen(object)).to.be.true - expect(Object.isFrozen(object[0])).to.be.true - }) - - it('ignores functions', () => { - const object = { foo: () => 1 } - u.freeze(object) - - expect(Object.isFrozen(object.foo)).to.be.false - }) - - it('ignores regexps', () => { - const object = { foo: /\d/ } - u.freeze(object) - - expect(Object.isFrozen(object.foo)).to.be.false - }) - - it('does not freeze children if the parent is already frozen', () => { - const object = { foo: {} } - Object.freeze(object) - u.freeze(object) - - expect(Object.isFrozen(object.foo)).to.be.false - }) - - it('does not freeze in production', () => { - process.env.NODE_ENV = 'production' - - const object = {} - u.freeze(object) - - expect(Object.isFrozen(object)).to.be.false - }) - - it('handles null objects', () => { - const object = { foo: null } - u.freeze(object) - expect(Object.isFrozen(object)).to.be.true - }) - - it('returns the same object', () => { - const object = {} - const result = u.freeze(object) - expect(result).to.equal(object) - }) -}) diff --git a/test/if-spec.js b/test/if-spec.js deleted file mode 100644 index e16066a..0000000 --- a/test/if-spec.js +++ /dev/null @@ -1,47 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.if', () => { - it('does not update if the predicate is false', () => { - const object = { a: 0 } - let result = u.if(false, { b: 1 }, object) - expect(result).to.eql(object) - - result = u({ x: u.if(false, 1) }, {}) - expect(result).to.eql({}) - }) - - it('does update if the predicate is true', () => { - const object = { a: 0 } - const result = u.if(true, { b: 1 }, object) - expect(result).to.eql({ a: 0, b: 1 }) - }) - - it('will use the result of a function passed as a predicate', () => { - const object = { a: 0 } - const aIsThree = (x) => x.a === 3 - const result = u.if(aIsThree, { b: 1 }, object) - - expect(result).to.eql({ a: 0 }) - }) - - it('can be partially applied', () => { - const object = { a: 2 } - const isEven = (x) => x % 2 === 0 - const inc = (x) => x + 1 - - const result = u( - { - a: u.if(isEven, inc), - }, - object - ) - - expect(result).to.eql({ a: 3 }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.if(true, {}, {}))).to.be.true - expect(Object.isFrozen(u.if(false, {}, {}))).to.be.true - }) -}) diff --git a/test/ifElse-spec.js b/test/ifElse-spec.js deleted file mode 100644 index f66cd67..0000000 --- a/test/ifElse-spec.js +++ /dev/null @@ -1,45 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.ifElse', () => { - it('does updates with the else if the predicate is false', () => { - const object = { a: 0 } - const result = u.ifElse(false, { b: 1 }, { b: 2 }, object) - expect(result).to.eql({ a: 0, b: 2 }) - }) - - it('updates with the true update if the predicate is true', () => { - const object = { a: 0 } - const result = u.ifElse(true, { b: 1 }, { b: 4 }, object) - expect(result).to.eql({ a: 0, b: 1 }) - }) - - it('will use the result of a function passed as a predicate', () => { - const object = { a: 0 } - const aIsThree = (x) => x.a === 3 - const result = u.ifElse(aIsThree, { b: 1 }, { b: 4 }, object) - - expect(result).to.eql({ a: 0, b: 4 }) - }) - - it('can be partially applied', () => { - const object = { a: 2 } - const isEven = (x) => x % 2 === 0 - const inc = (x) => x + 1 - const dec = (x) => x - 1 - - const result = u( - { - a: u.ifElse(isEven, inc, dec), - }, - object - ) - - expect(result).to.eql({ a: 3 }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.ifElse(true, {}, {}, {}))).to.be.true - expect(Object.isFrozen(u.ifElse(false, {}, {}, {}))).to.be.true - }) -}) diff --git a/test/is-spec.js b/test/is-spec.js deleted file mode 100644 index 584dd12..0000000 --- a/test/is-spec.js +++ /dev/null @@ -1,59 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.is', () => { - it('returns true if path matches a value predicate', () => { - const result = u.is('a.b', 4, { a: { b: 4 } }) - expect(result).to.be.true - }) - - it('returns true if path matches a function predicate', () => { - const isEven = (x) => x % 2 === 0 - const result = u.is('a.b', isEven, { a: { b: 6 } }) - expect(result).to.be.true - }) - - it('returns false if path matches a value predicate', () => { - const result = u.is('a.b', 4, { a: { b: 5 } }) - expect(result).to.be.false - }) - - it('returns false if path matches a function predicate', () => { - const isEven = (x) => x % 2 === 0 - const result = u.is('a.b', isEven, { a: { b: 7 } }) - expect(result).to.be.false - }) - - it('returns false if the path does not exist', () => { - const result = u.is('a.b.c.d', 4, { a: { b: {} } }) - expect(result).to.be.false - }) - - it('can test for undefined', () => { - const result = u.is('a.b.c', undefined, { a: { b: {} } }) - expect(result).to.be.true - }) - - it('tests the actual object if a blank path is given', () => { - const result = u.is('', 4, 4) - expect(result).to.be.true - }) - - it('can use arrays as paths', () => { - const result = u.is(['a', 'b'], 4, { a: { b: 4 } }) - expect(result).to.be.true - }) - - it('can include array indexes in paths', () => { - let result = u.is('a.1.b', 4, { a: [{}, { b: 4 }] }) - expect(result).to.be.true - - result = u.is(['a', 1, 'b'], 4, { a: [{}, { b: 4 }] }) - expect(result).to.be.true - }) - - it('can be partially applied', () => { - const result = u.is('a.b')(4)({ a: { b: 4 } }) - expect(result).to.be.true - }) -}) diff --git a/test/map-spec.js b/test/map-spec.js deleted file mode 100644 index d2c4bf3..0000000 --- a/test/map-spec.js +++ /dev/null @@ -1,75 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.map', () => { - it('applies updates to each item in an array', () => { - const object = [0, 1, 2] - const inc = (x) => x + 1 - const result = u.map(inc, object) - - expect(result).to.eql([1, 2, 3]) - }) - - it('applies updates to each value in an object', () => { - const object = { a: 0, b: 1, c: 2 } - const inc = (x) => x + 1 - const result = u.map(inc, object) - - expect(result).to.eql({ a: 1, b: 2, c: 3 }) - }) - - it('can update with a regular updates object', () => { - const object = [{ a: 0 }, { a: 0 }] - const result = u.map({ a: 1 }, object) - - expect(result).to.eql([{ a: 1 }, { a: 1 }]) - }) - - it('returns the same object if no updates are made', () => { - const array = [0, 1] - const ident = (x) => x - let result = u.map(ident, array) - - expect(result).to.equal(array) - - const object = { a: 0 } - result = u.map(ident, object) - - expect(result).to.equal(object) - }) - - it('passes the key or index as the second parameter to the iteratee', () => { - const object = { - a: { x: 0 }, - b: [3, 3], - } - const setToKey = (_, key) => key - const result = u.map(u.map(setToKey), object) - - expect(result).to.eql({ - a: { x: 'x' }, - b: [0, 1], - }) - }) - - it('can be partially applied', () => { - const object = { - b: [3, 3], - } - const setToKey = (_, key) => key - const result = u( - { - b: u.map(setToKey), - }, - object - ) - - expect(result).to.eql({ - b: [0, 1], - }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.map({}, {}))).to.be.true - }) -}) diff --git a/test/omit-spec.js b/test/omit-spec.js deleted file mode 100644 index bb645a0..0000000 --- a/test/omit-spec.js +++ /dev/null @@ -1,14 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.omit', () => { - it('can omit a key', () => { - const result = u({ foo: u.omit('bar') }, { foo: { bar: 7 } }) - - expect(result).to.eql({ foo: {} }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.omit('a', {}))).to.be.true - }) -}) diff --git a/test/omitBy-spec.js b/test/omitBy-spec.js deleted file mode 100644 index b3260ee..0000000 --- a/test/omitBy-spec.js +++ /dev/null @@ -1,18 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.omitBy', () => { - it('can omitBy with a function', () => { - const predicate = (value, key) => value === 7 && key === 'bar' - const result = u( - { foo: u.omitBy(predicate) }, - { foo: { bar: 7, baz: 'a' } } - ) - - expect(result).to.eql({ foo: { baz: 'a' } }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.omit('a', {}))).to.be.true - }) -}) diff --git a/test/reject-spec.js b/test/reject-spec.js deleted file mode 100644 index 0fe5f03..0000000 --- a/test/reject-spec.js +++ /dev/null @@ -1,44 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.reject', () => { - it('can reject by index', () => { - const result = u.reject((_, index) => index === 1, [3, 4, 5]) - - expect(result).to.eql([3, 5]) - }) - - it('can reject with callback shorthand', () => { - const result = u.reject('bad', [{ bad: true }, { bad: false }]) - - expect(result).to.eql([{ bad: false }]) - }) - - it("returns the same instance if reject doesn't make changes", () => { - const object = { foo: [1, 2, 3] } - const result = u( - { - foo: u.reject((x) => x === 'Justin Bieber'), - }, - object - ) - - expect(result).to.equal(object) - }) - - it('returns a different instance if reject makes changes', () => { - const object = { foo: [1, 2, 3, 4] } - const result = u( - { - foo: u.reject((x) => x === 4), - }, - object - ) - - expect(result).to.not.equal(object) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.reject('a', []))).to.be.true - }) -}) diff --git a/test/updateIn-spec.js b/test/updateIn-spec.js deleted file mode 100644 index 56e1ffe..0000000 --- a/test/updateIn-spec.js +++ /dev/null @@ -1,61 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.updateIn', () => { - it('can update a single path described with a string', () => { - const object = { a: { b: 0 } } - const result = u.updateIn('a.b', 3, object) - expect(result).to.eql({ a: { b: 3 } }) - }) - - it('can update a single path described with a string with a function', () => { - const inc = (x) => x + 1 - const object = { a: { b: 0 } } - const result = u.updateIn('a.b', inc, object) - expect(result).to.eql({ a: { b: 1 } }) - }) - - it('can update a single path described with an array', () => { - const object = { a: { b: 0 } } - const result = u.updateIn(['a', 'b'], 3, object) - expect(result).to.eql({ a: { b: 3 } }) - }) - - it('can update arrays', () => { - const object = { a: [0, 0, 0] } - const result = u.updateIn('a.1', 3, object) - expect(result).to.eql({ a: [0, 3, 0] }) - }) - - it('can be partially applied', () => { - const object = { a: { b: 0 } } - const result = u.updateIn('a.b')(3)(object) - expect(result).to.eql({ a: { b: 3 } }) - }) - - it('replaces the object outright if the path is empty', () => { - const object = {} - const result = u.updateIn('', 3, object) - expect(result).to.equal(3) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.updateIn('a', 0, {}))).to.be.true - }) - - it('can multiple elements of an array with *', () => { - let object = { a: [{ b: 0 }, { b: 1 }, { b: 2 }] } - let result = u.updateIn('a.*.b', (x) => x + 1, object) - expect(result).to.eql({ a: [{ b: 1 }, { b: 2 }, { b: 3 }] }) - - object = { a: [0, 1, 2] } - result = u.updateIn(['a', '*'], (x) => x + 1, object) - expect(result).to.eql({ a: [1, 2, 3] }) - }) - - it('can update properties named *', () => { - const object = { '*': 1, x: 1 } - const result = u.updateIn('*', (x) => x + 1, object) - expect(result).to.eql({ '*': 2, x: 1 }) - }) -}) diff --git a/test/updeep-spec.js b/test/updeep-spec.js deleted file mode 100644 index aecab25..0000000 --- a/test/updeep-spec.js +++ /dev/null @@ -1,174 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('updeep', () => { - it('does not change anything if no updates are specified', () => { - const object = { foo: 3, bar: [7, 5] } - const result = u({}, object) - - expect(result).to.equal(object) - }) - - it('can update with fixed values', () => { - const object = { foo: 3, bar: [7, 5] } - const result = u({ foo: 4 }, object) - - expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }) - }) - - it("returns the same instance if an update doesn't make changes", () => { - const object = { foo: 3 } - const result = u({ foo: 3 }, object) - - expect(result).to.equal(object) - }) - - it('can update a nested structure', () => { - const object = { foo: { bar: 7, bam: 3 }, baz: 32 } - const result = u({ foo: { bar: 8 } }, object) - - expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }) - }) - - it('can update arrays', () => { - const object = [1, 2, 3] - const result = u({ 1: 7 }, object) - - expect(result).to.deep.equal([1, 7, 3]) - }) - - it('replaces the object outright if updates are a constant', () => { - expect(u(3, {})).to.equal(3) - expect(u(null, {})).to.be.null - }) - - it('can add an element to an array', () => { - const object = [] - const result = u({ 0: 3 }, object) - - expect(result).to.eql([3]) - }) - - it('can update nested arrays', () => { - const object = { foo: [1, 2, 3], bar: 9 } - const result = u({ foo: { 1: 7 } }, object) - - expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }) - }) - - it('can use functions to update values', () => { - const inc = (i) => i + 1 - const object = { foo: 3, bar: 4, baz: 7 } - const result = u({ foo: inc, bar: inc }, object) - - expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }) - }) - - it('can be partially applied', () => { - const inc = (i) => i + 1 - const object = { foo: 3 } - const incFoo = u({ foo: inc }) - - const result = incFoo(object) - - expect(result).to.deep.equal({ foo: 4 }) - }) - - it('passes additional arguments on to updates if it is a function', () => { - const func = (_, x) => x - const result = u(func, 0, 4) - - expect(result).to.equal(4) - }) - - it('can update if the value is an array', () => { - const object = {} - const result = u({ foo: [0, 1] }, object) - - expect(result).to.deep.equal({ foo: [0, 1] }) - }) - - it('can update when original object is undefined', () => { - const result = u({ foo: [0, 1] }, undefined) - - expect(result).to.deep.equal({ foo: [0, 1] }) - }) - - it('can take a function as the updater', () => { - const result = u((i) => i + 1, 7) - - expect(result).to.eql(8) - }) - - it('deeply freezes the result', () => { - const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }) - - expect(Object.isFrozen(result)).to.be.true - expect(Object.isFrozen(result.foo)).to.be.true - }) - - it('assigns null values', () => { - expect(u({ isNull: null }, {})).to.eql({ isNull: null }) - }) - - it('can use a placeholder to partially apply', () => { - function increment(i) { - return i + 1 - } - const updateJoe = u(u._, { name: 'Joe Merrill', age: 21 }) - const result = updateJoe({ age: increment }) - - expect(result).to.eql({ name: 'Joe Merrill', age: 22 }) - }) - - it('defaults to an empty object when null or undefined', () => { - let result = u({ a: { b: 0 } }, { a: null }) - expect(result).to.eql({ a: { b: 0 } }) - - result = u({ a: { b: 0 } }, { a: undefined }) - expect(result).to.eql({ a: { b: 0 } }) - - result = u({ a: { b: 0 } }, {}) - expect(result).to.eql({ a: { b: 0 } }) - }) - - it('preserves empty objects when empty updates are specified', () => { - const result = u({ a: {} }, {}) - expect(result).to.eql({ a: {} }) - }) - - it('works with date objects', () => { - const date = new Date() - const result = u({ created: date }, {}) - expect(result).to.eql({ created: date }) - }) - - const expectU = (update, orig, expected) => - expect(update(orig)).to.eql(expected) - - describe('u.omitted', () => { - it('omit properties via u.omitted', () => { - expectU(u({ a: u.omitted, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }) - }) - - it('omit array and object properties', () => { - expectU( - u({ a: u.omitted, b: 'stuff', c: u.omitted }), - { a: [1, 2, 3], b: 'orig', c: { z: 'bar' } }, - { b: 'stuff' } - ) - }) - - it('deep omit', () => { - expectU( - u({ a: { b: u.omitted, c: 'stuff' } }), - { a: { b: 'foo', z: 'bar' } }, - { a: { z: 'bar', c: 'stuff' } } - ) - }) - - it('omitting an array item filters it out', () => { - expectU(u({ 1: u.omitted }), ['a', 'b', 'c'], ['a', 'c']) - }) - }) -}) diff --git a/test/util/splitPath-spec.js b/test/util/splitPath-spec.js deleted file mode 100644 index b0d5bd5..0000000 --- a/test/util/splitPath-spec.js +++ /dev/null @@ -1,22 +0,0 @@ -import { expect } from 'chai' -import splitPath from '../../lib/util/splitPath' - -describe('splitPath', () => { - it('treats a number as a single step path', () => { - const path = 1 - const result = splitPath(path) - expect(result).to.deep.equal(['1']) - }) - - it('handles arrays', () => { - const path = ['foo', 'bar', 'x'] - const result = splitPath(path) - expect(result).to.equal(path) - }) - - it('handles strings separated by dots', () => { - const path = 'bar.0.y' - const result = splitPath(path) - expect(result).to.deep.equal(['bar', '0', 'y']) - }) -}) diff --git a/test/withDefault-spec.js b/test/withDefault-spec.js deleted file mode 100644 index eb77574..0000000 --- a/test/withDefault-spec.js +++ /dev/null @@ -1,34 +0,0 @@ -import { expect } from 'chai' -import u from '../lib' - -describe('u.withDefault', () => { - it('uses the default as the basis for the update if the object is undefined', () => { - const inc = (x) => x + 1 - const result = u.withDefault({ a: 0 }, { a: inc }, undefined) - - expect(result).to.eql({ a: 1 }) - }) - - it('uses ignores the default if the object is defined', () => { - const inc = (x) => x + 1 - const result = u.withDefault({ a: 0 }, { a: inc }, { a: 3 }) - - expect(result).to.eql({ a: 4 }) - }) - - it('can be partially applied', () => { - const object = {} - const result = u( - { - foo: u.withDefault([], { 0: 'bar' }), - }, - object - ) - - expect(result).to.eql({ foo: ['bar'] }) - }) - - it('freezes the result', () => { - expect(Object.isFrozen(u.withDefault({}, { a: 1 })(undefined))).to.be.true - }) -}) diff --git a/tsconfig-cjs.json b/tsconfig-cjs.json new file mode 100644 index 0000000..1cf722f --- /dev/null +++ b/tsconfig-cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "commonjs/", + "target": "es2015" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e4c4270 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,105 @@ +{ + "exclude": ["test"], + "include": ["src/"], + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + "incremental": true /* Save .tsbuildinfo files to allow for incremental compilation of projects. */, + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "nodenext" /* Specify what module code is generated. */, + "rootDir": "./src" /* Specify the root folder within your source files. */, + // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, + "checkJs": true /* Enable error reporting in type-checked JavaScript files. */, + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */, + "declarationMap": true /* Create sourcemaps for d.ts files. */, + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + "sourceMap": true /* Create source map files for emitted JavaScript files. */, + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./esm" /* Specify an output folder for all emitted files. */, + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": false /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/types/if.d.ts b/types/if.d.ts deleted file mode 100644 index b1ba4bf..0000000 --- a/types/if.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Predicate, MergedUpdate, TruePredicate, FalsePredicate } from './types'; -export declare function uIf(predicate: FalsePredicate, trueUpdates: TU, object: O): O; -export declare function uIf(predicate: TruePredicate, trueUpdates: TU, object: O): MergedUpdate; -interface CurriedIf { - (predicate: TruePredicate, trueUpdates: TU, object: O): MergedUpdate; - (predicate: FalsePredicate, trueUpdates: TU, object: O): O; - (predicate: Predicate, trueUpdates: TU, object: O): MergedUpdate | O; - (predicate: TruePredicate, trueUpdates: TU): (object: O) => MergedUpdate; - (predicate: FalsePredicate, trueUpdates: TU): (object: O) => O; - (predicate: Predicate, trueUpdates: TU): (object: O) => MergedUpdate | O; -} -declare const _default: CurriedIf; -export default _default; diff --git a/types/ifElse.d.ts b/types/ifElse.d.ts deleted file mode 100644 index 6eea6b1..0000000 --- a/types/ifElse.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Predicate, MergedUpdate, FalsePredicate, TruePredicate } from './types'; -export declare function updateIfElse(predicate: TruePredicate, trueUpdates: TU, falseUpdates: FU, object: S): MergedUpdate; -export declare function updateIfElse(predicate: FalsePredicate, trueUpdates: TU, falseUpdates: FU, object: S): MergedUpdate; -interface CurriedIfElse { - (predicate: TruePredicate, trueUpdates: TU, falseUpdates: FU, object: S): MergedUpdate; - (predicate: FalsePredicate, trueUpdates: TU, falseUpdates: FU, object: S): MergedUpdate; - (predicate: Predicate, trueUpdates: TU, falseUpdates: FU, object: S): MergedUpdate | MergedUpdate; - (predicate: TruePredicate, trueUpdates: TU, falseUpdates: FU): (object: S) => MergedUpdate; - (predicate: FalsePredicate, trueUpdates: TU, falseUpdates: FU): (object: S) => MergedUpdate; - (predicate: Predicate, trueUpdates: TU, falseUpdates: FU): (object: S) => MergedUpdate | MergedUpdate; -} -declare const _default: CurriedIfElse; -export default _default; diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index 15a1c34..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { constant } from './constant'; -import freeze from './freeze'; -import is from './is'; -import _if from './if'; -import ifElse from './ifElse'; -import map from './map'; -import omit from './omit'; -import omitBy from './omitBy'; -import reject from './reject'; -import update, { omitted } from './update'; -import updateIn from './updateIn'; -import withDefault from './withDefault'; -import { _ as placeholder } from './util/curry'; -declare const u: { - omit: typeof omit; - omitBy: typeof omitBy; - constant: typeof constant; - freeze: typeof freeze; - is: typeof is; - update: typeof update; - updateIn: typeof updateIn; - reject: typeof reject; - map: typeof map; - withDefault: typeof withDefault; - ifElse: typeof ifElse; - if: typeof _if; - omitted: typeof omitted; - _: typeof placeholder; -} & typeof update; -export default u; diff --git a/types/is.d.ts b/types/is.d.ts deleted file mode 100644 index a3632c1..0000000 --- a/types/is.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Path } from './types'; -export declare function is(path: Path, predicate: any, object: any): boolean; -interface CurriedIs { - (path: Path, predicate: any, object: any): boolean; - (path: Path, predicate: any): (object: any) => boolean; - (path: Path): (predicate: any) => (object: any) => boolean; -} -declare const _default: CurriedIs; -export default _default; diff --git a/types/map.d.ts b/types/map.d.ts deleted file mode 100644 index e8f1549..0000000 --- a/types/map.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { MergedUpdate } from './types'; -declare type Mapped = { - [K in keyof O]: MergedUpdate; -}; -export declare function map(iteratee: I, object: O): Mapped; -interface CurriedMap { - (iteratee: I, object: O): Mapped; - (iteratee: I): (object: O) => Mapped; -} -declare const _default: CurriedMap; -export default _default; diff --git a/types/omit.d.ts b/types/omit.d.ts deleted file mode 100644 index 78bb67b..0000000 --- a/types/omit.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export declare function omit(predicate: string[] | string, collection: object): object; -interface CurriedOmit { - (predicate: string[] | string, collection: object): object; - (predicate: string[] | string): (collection: object) => object; -} -declare const _default: CurriedOmit; -export default _default; diff --git a/types/omitBy.d.ts b/types/omitBy.d.ts deleted file mode 100644 index 51e6cb8..0000000 --- a/types/omitBy.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export declare function omitBy boolean>(predicate: P, collection: C): object; -interface CurriedOmitBy { - boolean>(predicate: P, collection: C): object; - boolean>(predicate: P): (collection: C) => object; -} -declare const _default: CurriedOmitBy; -export default _default; diff --git a/types/reject.d.ts b/types/reject.d.ts deleted file mode 100644 index 2e7938c..0000000 --- a/types/reject.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export declare function reject(predicate: any, collection: C): C extends any[] ? C : object; -interface CurriedReject { - (predicate: any, collection: C): C; - (predicate: any, collection: C): Array; - (predicate: any): (collection: C) => C; - (predicate: any): (collection: C) => Array; -} -declare const _default: CurriedReject; -export default _default; diff --git a/types/tests.ts b/types/tests.ts deleted file mode 100644 index f9f5f3a..0000000 --- a/types/tests.ts +++ /dev/null @@ -1,61 +0,0 @@ -// TypeScript Version: 3.2.2 -import u from "updeep"; - -u.omitted('whatever'); // $ExpectType { __omitted: boolean; } - -const obj = { this: 3 }; - -u(true, obj); // $ExpectType true -u(null, obj); // $ExpectType null -u(undefined, obj); // $ExpectType undefined -u("a specific string", obj); // $ExpectType "a specific string" - -u(true as const)(obj); // $ExpectType true -u(null)(obj); // $ExpectType null -u(undefined)(obj); // $ExpectType undefined -u("a specific string" as const)(obj); // $ExpectType "a specific string" - -const aString = "a" + "b"; - -u(aString, obj); // $ExpectType string - -u((i: number) => "foo" + i, 1); // $ExpecType string -u((i: number) => "foo" + i, "bar"); - -// update is object -u({ this: 2 }, true); // $ExpectType UpdateReturnMap<{ this: number; }> -u({ this: 2 })(true); // $ExpectType UpdateReturnMap<{ this: number; }> - -u({ this: 2 }, { this: 3 }); // $ExpectType object -u({ this: 2 })({ that: 3 }); // $ExpectType object - -u({ this: 2 })(true); // UpdateReturnMap<{ this: number; }> -u({ this: 2 })({ that: 3 }); // $ExpectType object - - -u.ifElse(false as boolean, { a: 1 }, { a: 2 }, { a: 3 }); // $ExpectType object -u.ifElse(false as boolean, "foo" as const, 3 as const, { a: 3 }); // $ExpectType 3 | "foo" -u.ifElse(false, "foo" as const, 3 as const, { a: 3 }); // $ExpectType 3 -u.ifElse(true, "foo" as const, 3 as const, { a: 3 }); // $ExpectType "foo" - -// *** map *** -const inc = (i: number) => i+1; - -u.map(inc, [1,2,3]); // $ExpectType number[] -u.map(inc, ["potato"]); // $ExpectType number[] -u.map({a:1},{a:2}); // $ExpectType Mapped<{ a: number; }, { a: number; }> - -u.omit('bar', { }); // $ExpectType object -u.omit(['bar'], { }); // $ExpectType object - -u.omitBy([ 'banana' ], { } ); // $ExpectError - -// *** constant *** - -// $ExpectType { banana: number; } -u.constant({ banana: 1 })('foo'); - -/// *** freeze *** - -// $ExpectType { potato: number; } -u.freeze({ potato: 1 }); diff --git a/types/tsconfig.json b/types/tsconfig.json deleted file mode 100644 index 80ecf93..0000000 --- a/types/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs", - "lib": ["es6"], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "noEmit": true, - - "baseUrl": "./", - "paths": { "updeep": ["."] } - } -} diff --git a/types/tslint.json b/types/tslint.json deleted file mode 100644 index c0c24d9..0000000 --- a/types/tslint.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "dtslint/dtslint.json", - "rules": { - "trim-file": false, - "no-consecutive-blank-lines": false, - "typedef-whitespace": false, - "whitespace": false, - "space-within-parens": false, - "prefer-declare-function": false, - "no-unnecessary-generics": false, - "strict-export-declare-modifiers": false } } diff --git a/types/types.d.ts b/types/types.d.ts deleted file mode 100644 index f0640af..0000000 --- a/types/types.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -export declare type Updates = any; -export declare type Source = any; -export declare type PathPart = number | string; -export declare type Path = PathPart | PathPart[]; -export declare type TruePredicate = true | ((a: S) => true); -export declare type FalsePredicate = false | ((a: S) => false); -export declare type Predicate = boolean | ((arg: S) => boolean); -export declare type MergedUpdate = O extends object ? (UpdateReturnType extends object ? object : UpdateReturnType) : UpdateReturnType; -export declare type UpdateReturnMap = { - [K in keyof T]: UpdateReturnType; -}; -export declare type UpdateReturnType = U extends (object: any) => any ? ReturnType : U extends object ? UpdateReturnMap : U; -export declare type ReturningFunction = (...args: any[]) => any; -export declare type ArgumentsType any> = F extends (...args: infer A) => any ? A : never; diff --git a/types/update.d.ts b/types/update.d.ts deleted file mode 100644 index 7fe709a..0000000 --- a/types/update.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { MergedUpdate, UpdateReturnType } from './types'; -export declare const omitted: (...args: any[]) => { - __omitted: boolean; -}; -/** - * Recursively update an object or array. - * - * Can update with values: - * update({ foo: 3 }, { foo: 1, bar: 2 }); - * // => { foo: 3, bar: 2 } - * - * Or with a function: - * update({ foo: x => (x + 1) }, { foo: 2 }); - * // => { foo: 3 } - * - */ -export declare function update(updates: U extends object ? never : U, object: any): U; -export declare function update(updates: U, object: O extends object ? never : O): UpdateReturnType; -export declare function update(updates: U, object: O, ...args: any[]): MergedUpdate; -interface CurriedUpdate1 { - (object: O extends object ? never : O): UpdateReturnType; - (object: O, ...args: any[]): MergedUpdate; -} -interface CurriedUpdate { - (updates: U extends object ? never : U, object: any): U; - (updates: U, object: O extends object ? never : O): UpdateReturnType; - (updates: U, object: O, ...args: any[]): MergedUpdate; - (updates: U extends object ? never : U): (object: any) => U; - (updates: U): CurriedUpdate1; -} -declare const _default: CurriedUpdate; -export default _default; diff --git a/types/updateIn.d.ts b/types/updateIn.d.ts deleted file mode 100644 index af9edc3..0000000 --- a/types/updateIn.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Path } from './types'; -export declare function updateIn(path: Path, value: any, object: any): any; -declare const _default: { - (a: any): { - (a: any): (a: any) => any; - (a: any, b: any): any; - }; - (a: any, b: any): (a: any) => any; - (a: any, b: any, c: any): any; -}; -export default _default; diff --git a/types/util/curry.d.ts b/types/util/curry.d.ts deleted file mode 100644 index 546b610..0000000 --- a/types/util/curry.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -export declare const _ = "@@updeep/placeholder"; -interface SprawlingCurry { - (...args: A): R; - (): SprawlingCurry; -} -export declare function curry1(fn: (a: A) => R): SprawlingCurry; -export declare function curry1(fn: (a: A, ...args: any) => R): SprawlingCurry; -export declare function curry2(fn: any): any; -export declare function curry3(fn: any): any; -export declare function curry4(fn: any): any; -export default function curry(fn: (...args: any[]) => any, length?: 1 | 2 | 3 | 4): any; -export {}; diff --git a/types/util/splitPath.d.ts b/types/util/splitPath.d.ts deleted file mode 100644 index 94529e7..0000000 --- a/types/util/splitPath.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { Path } from '../types'; -export default function splitPath(path: Path): Array; diff --git a/types/withDefault.d.ts b/types/withDefault.d.ts deleted file mode 100644 index c38c020..0000000 --- a/types/withDefault.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { MergedUpdate } from './types'; -export declare function withDefault(defaultValue: D, updates: U, object: O): MergedUpdate; -interface CurriedWithDefault { - (defaultValue: D, updates: U, object: O): MergedUpdate; - (defaultValue: D, updates: U): (obj: O) => MergedUpdate; -} -declare const _default: CurriedWithDefault; -export default _default; diff --git a/types/wrap.d.ts b/types/wrap.d.ts deleted file mode 100644 index 955b2cf..0000000 --- a/types/wrap.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { ReturningFunction } from './types'; -export default function wrap(func: F, l?: N): any; diff --git a/vitest.config.js b/vitest.config.js new file mode 100644 index 0000000..ab4f13e --- /dev/null +++ b/vitest.config.js @@ -0,0 +1,5 @@ +export default { + test: { + include: ['src/**/*.test.ts'], + }, +}; diff --git a/wallaby.conf.js b/wallaby.conf.js deleted file mode 100644 index c57deba..0000000 --- a/wallaby.conf.js +++ /dev/null @@ -1,16 +0,0 @@ -/*eslint-disable */ -var babel = require("babel"); - -module.exports = function(wallaby) { - return { - files: ["lib/**/*.js"], - tests: ["test/**/*-spec.js"], - compilers: { - "**/*.js": wallaby.compilers.babel({ babel: babel, stage: 1 }) - }, - env: { - type: "node" - }, - testFramework: "mocha" - }; -};