JSPM

  • Created
  • Published
  • Downloads 1493
  • Score
    100M100P100Q108105F
  • License MIT

Merge and flatten any arrays found in all values within plain objects

Package Exports

  • object-flatten-all-arrays

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-flatten-all-arrays) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

object-flatten-all-arrays

Merge and flatten any arrays found in all values within plain objects

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-flatten-all-arrays
// consume as a CommonJS require:
const flattenAllArrays = require("object-flatten-all-arrays");
// or as an ES Module:
import flattenAllArrays from "object-flatten-all-arrays";

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-flatten-all-arrays.cjs.js 2 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/object-flatten-all-arrays.esm.js 2 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/object-flatten-all-arrays.umd.js 36 KB

⬆ back to top

Purpose

Recursively traverse the cloned input and merge all plain objects within each array.

For example

const flattenAllArrays = require("object-flatten-all-arrays");
const object = {
  a: "a",
  b: "b",
  c: [
    {
      b: "b",
      a: "a"
    },
    {
      d: "d",
      c: "c"
    }
  ]
};
const flattened = flattenAllArrays(object);
console.log("flattened = " + JSON.stringify(flattened, null, 4));
// => {
// a: 'a',
// b: 'b',
// c: [
//   {
//     a: 'a',
//     b: 'b',
//     c: 'c',
//     d: 'd'
//   }
// ]}

⬆ back to top

API

flatten(input[, options])

Returns the same type thing as given input, only with arrays (recursively) flattened.

API - Input

None of the input arguments are mutated. Their clones are being used instead.

Input argument Type Obligatory? Description
input Whatever yes AST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
options Plain object no Set the options in this object. See below for keys.
options object's key Type Obligatory? Default Description
{
flattenArraysContainingStringsToBeEmpty Boolean no false If any arrays contain strings, flatten them to be empty thing. This is turned off by default, but it's what you actually need most of the time.
}

⬆ back to top

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