JSPM

  • Created
  • Published
  • Downloads 48
  • Score
    100M100P100Q102393F
  • License MIT

Recursively walk the input and set all found values in plain objects to something

Package Exports

  • object-set-all-values-to

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-set-all-values-to) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

object-set-all-values-to

Recursively walk the input and set all found values in plain objects to something

Minimum Node version required Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Table of Contents

Install

npm i object-set-all-values-to
// consume as a CommonJS require:
const setAllValuesTo = require("object-set-all-values-to");
// or as an ES Module:
import setAllValuesTo from "object-set-all-values-to";

Here's what you'll get:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/object-set-all-values-to.cjs.js 2 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/object-set-all-values-to.esm.js 1 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/object-set-all-values-to.umd.js 11 KB

⬆ back to top

Purpose

Take any input: nested array, nested plain object or whatever really, no matter how deeply nested. Walk through it recursively and if you find any plain objects, assign all their keys to a given second input's argument OR default, false.

It does not mutate the input arguments. Operations are done on a cloned input.

I needed this library to overwrite all values to be false on JSON schema objects, so that later when I copy from key/value pairs from schema, values are equal to false and I don't need to prep them further.

This library is well-tested and is being used in commercial projects.

⬆ back to top

Use

const setAllValuesTo = require("object-set-all-values-to");

console.log(setAllValuesTo({ a: "b", c: "d" }));
// => {a: false, c: false}

console.log(setAllValuesTo({ a: "b", c: "d" }, "x"));
// => {a: 'x', c: 'x'}

console.log(setAllValuesTo({ a: "b", c: "d" }, ["x"]));
// => {a: ['x'], c: ['x']}

⬆ back to top

API

setAllValuesTo(input, value);

API - Input

Input argument Type Obligatory? Default Description
input Whatever yes undefined AST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
value Whatever no false Assign all the found plain object values to this

⬆ back to top

API - Output

Same thing that you gave in the first argument, except with values overwritten (where applicable).

Contributing

  • If you see an error, raise an issue.
  • If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
  • If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
  • If you want to contribute some code, fork the monorepo via BitBucket, then write code, then file a pull request via BitBucket. We'll merge it in and release.

In monorepo, npm libraries are located in packages/ folder. Inside, the source code is located either in src/ folder (normal npm library) or in the root, cli.js (if it's a command line application).

The npm script "dev", the "dev": "rollup -c --dev --silent" builds the development version retaining all console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.logs.

⬆ back to top

Licence

MIT License

Copyright (c) 2015-2019 Roy Revelt and other contributors