Code Monkey home page Code Monkey logo

javascript-unwind's Introduction

javascript-unwind

Current npm version Current npm download count CircleCI Compatible with ES5 and greater

A very simple native function for unwinding a collection by a property, like MongoDB's $unwind function.

Features

  • This function is immutable. Your original collection doesn't change.
  • It works for both primitives and complex values.
  • It works for nested arrays, using dot-notation. Like a.b.c.
  • It works in ES5-compatible environments, without transpiling.
  • Typings available for TypeScript.

Installation

$ npm i javascript-unwind --save

How to use

Shallow level

  const unwind = require('javascript-unwind');

  const collection = [
    { a: [{ x: 1 }, { x: 2 }], b: 123 },
    { a: [{ x: 3 }, { x: 4 }], b: 785, c: 368 },
  ];

  console.log(unwind(collection, 'a'));

Output:

[ { a: { x: 1 }, b: 123 },
 { a: { x: 2 }, b: 123 },
 { a: { x: 3 }, b: 785, c: 368 },
 { a: { x: 4 }, b: 785, c: 368 } ]

Deep level

  const unwind = require('javascript-unwind');

  const collection = [{ 
      name: 'a1', 
      b: [{ 
          name: 'b1', 
          c: [ { name: 'c1' }, { name: 'c2' } ] 
        }, { 
          name: 'b2', 
          c: [ { name: 'c3' }, { name: 'c4' } ] 
        }, 
      ], 
    }, { 
      name: 'a2', 
      b: [{ 
          name: 'b3', 
          c: [ { name: 'c4' }, { name: 'c5' } ]
        }, { 
          name: 'b4',
          c: [ { name: 'c6' }, { name: 'c7' } ] 
        }
      ]
    }
  ];

  console.log(unwind(collection, 'b.c'));

Output:

[
  {
    "name":"a1",
    "b":{
      "name":"b1",
      "c":{
        "name":"c1"
      }
    }
  },
  {
    "name":"a1",
    "b":{
      "name":"b1",
      "c":{
        "name":"c2"
      }
    }
  },
  {
    "name":"a1",
    "b":{
      "name":"b2",
      "c":{
        "name":"c3"
      }
    }
  },
  {
    "name":"a1",
    "b":{
      "name":"b2",
      "c":{
        "name":"c4"
      }
    }
  },
  {
    "name":"a2",
    "b":{
      "name":"b3",
      "c":{
        "name":"c4"
      }
    }
  },
  {
    "name":"a2",
    "b":{
      "name":"b3",
      "c":{
        "name":"c5"
      }
    }
  },
  {
    "name":"a2",
    "b":{
      "name":"b4",
      "c":{
        "name":"c6"
      }
    }
  },
  {
    "name":"a2",
    "b":{
      "name":"b4",
      "c":{
        "name":"c7"
      }
    }
  }
]

Roadmap and Bugs

See Issues.

javascript-unwind's People

Contributors

felipemonobe avatar ggondim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javascript-unwind's Issues

Can't import with webpack

Using webpack (v1.13.0), when importing javascript-unwind using ES6 importing style, throws an error:

import unwind from 'javascript/unwind';

Throws at webpack compilation:

ERROR in ./~/javascript-unwind/index.js
Module parse failed: /home/gustavo/repos/web/node_modules/javascript-unwind/index.js Unexpected token (19:8)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (19:8)
    at Parser.pp$4.raise (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp$3.parseExprAtom (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1822:12)
    at Parser.pp$3.parseExprSubscripts (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$3.parseExprList (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2165:22)
    at Parser.pp$3.parseSubscripts (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1741:35)
    at Parser.pp$3.parseExprSubscripts (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1718:17)
    at Parser.pp$3.parseMaybeUnary (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$2.parseSpread (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1384:26)
    at Parser.pp$3.parseExprList (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2160:22)
    at Parser.pp$3.parseExprAtom (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1801:28)
    at Parser.pp$3.parseExprSubscripts (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/gustavo/repos/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
 @ ./*********.controller.js 9:24-52

Be allowed to unwind object.array instead of array.array

It would be nice if unwind understood the following structure.

const collection = [
{name: 'a1',
b: {
name: 'b1',
c: [ { name: 'c1' }, { name: 'c2' } ]
}
},
{name: 'a2',
b: {
name: 'b2',
c: [ { name: 'c3' }, { name: 'c4' } ]
}
}
];

console.log(unwind(collection, 'b.c')); // An array within an object

if should return
[
{name: ‘a1’, b: {name: ‘b1’ c: {name: ‘c1’}}},
{name: ‘a1’, b: {name: ‘b1’ c: {name: ‘c2’}}},
{name: ‘a2’, b: {name: ‘b2’ c: {name: ‘c3’}}},
{name: ‘a2’, b: {name: ‘b2’ c: {name: ‘c4’}}}
]

MongoDB can handle it correctly.
// Stage 3
{
$unwind: {
path : "$b.c",
includeArrayIndex : "b.c.arrayIndex", // optional
preserveNullAndEmptyArrays : false // optional
}
},

Typescript definition

Hi, thanks for the library, it's small but neat and does exactly what is needed.
Would you consider creating the TypeScript definition ?

Thanks
Tom

Add more unit tests

Add tests to verify both shallow and deep methods, like the examples of README.md.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.