jbj-array
JBJ array module
Last updated 5 years ago by parmentf .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jbj-array 
SYNC missed versions from official npm registry.

JBJ array module

JBJ array module: complex actions implying arrays (mapping, mappingVar, zip, array2object, arrays2objects, coalesce, substring, getindex, getindexvar).

Contributors

Installation

$ npm install jbj-array

Usage

This JBJ module cannot be used alone. JBJ has to be installed.

var JBJ = require('jbj');
JBJ.use(require('jbj-array'));

Tests

Use mocha to run the tests.

$ npm install
$ npm test

Actions

Once the module is declared as used for JBJ, you can use the following actions:

mapping: object

Replace a value by the matching value in the object.

{
  "set": "one",
  "mapping": {
    "one": 1
  }
}
// output: 1
{
  "set": "FR",
  "mapping": {
    "US": "United States of America",
    "FR": "France"
  }
}
// output: "France"

Can also replace the values of an array with the matching values in the object.

{
  "set": [1, 2],
  "mapping": ["a","b","c"]
}
// output: ["b","c"]
{
  "set": ["a", "b"],
  "mapping": {
    "a": "Aha!",
    "b": "Baby"
  }
}
// output: ["Aha!","Baby"]

mappingVar: ["input","table"]

alias: combine

Replace the content of the input variable according to the content of the table variable.

var input = {
  "arg": { "a": "Aha!", "b": "Baby"},
  "input": "a"
};
var stylesheet = {
  "mappingVar": ["input", "arg"]
};
var output = JBJ.renderSync(stylesheet, input);
// output "Aha!";

array2object: [key, value]

Convert an array, which items have key and value properties, to an associative array (or object), which key properties are key values and values are value values.

Note: when the parameter is not a two items array, its default value is ["_id","value"].

Ex:

var stylesheet = {
  "set": [
    {
      "_id": "2007",
      "value": 538
    }, {
      "_id": "2008",
      "value": 577
    }, {
      "_id": "2009",
      "value": 611
  }],
  "array2object": true
};
// output = { "2007": 538, "2008": 577, "2009": 611 }

var stylesheet = {
  "set": [
    {
      "key": "2007",
      "val": 538
    }, {
      "key": "2008",
      "val": 577
    }, {
      "key": "2009",
      "val": 611
  }],
  "array2object": ["key","val"]
};
// output = { "2007": 538, "2008": 577, "2009": 611 }

arrays2objects: [key, value]

Convert an array of arrays (of 2 items), to an array of objects, where the first key if key and the second value). Defailt value of key: _id, default value of value: value.

Note: this is useful to prepare data from a CSV file to be treated with array2object.

Ex:

var stylesheet = {
  "set": [ [ "Afghanistan", "AFG" ],
           [ "Aland Islands", "ALA" ] ],
  "arrays2objects": ["key", "val"]
};
// output: [ { "key": "Afghanistan", "val": "AFG"},
//           { "key": "Aland Islands", "val": "ALA"} ]
var stylesheet = {
  "set": [ [ "Afghanistan", "AFG" ],
           [ "Aland Islands", "ALA" ] ],
  "arrays2objects": true
};
// output: [ { "_id": "Afghanistan", "value": "AFG"},
//           { "_id": "Aland Islands", "value": "ALA"} ]

zip: ["array1","array2"]

Join two arrays (which elements have an _id and a value keys).

var stylesheet = {
  "set": {
    "array1": [{"_id": "1", "value": 1},  {"_id": "2", "value": 2}],
    "array2": [{"_id": "1", "value": 10}, {"_id": "2", "value": 20}]
  },
  "zip": [ "array1", "array2" ]
};
var output = JBJ.renderSync(stylesheet);
// output: [ { _id: '1', array1: 1, array2: 10 },
//           { _id: '2', array1: 2, array2: 20 } ]

coalesce: none

Get the first non-undefined value

    var stylesheet = {
        "set" : [null, undefined, null, "a", "b"],
        "coalesce": true
    };
    // output : "a"

substring: [offset]|[offset, length]

aliases : substr

    var stylesheet = {
      "set"       : "20150310",
      "substring" : [4,2]
    };
    // output : "03"

getindex: property | index

aliases : getProperty, getproperty, getIndex

Get a property of an object, or an item of an array.

var stylesheet = {
  "set"        : [ "a", "b", "c" ],
  "getindex": "2"
};
// output : "c"
var stylesheet = {
  "set"        : { "a": 0, "b": 1, "c":2 },
  "getproperty": "b"
};
// output : 1

getindexvar: [ arrayName | objectName , propertyName | indexName ]

aliases : getPropertyVar, getpropertyvar, getIndexVar

Get a property of an object, or an item of an array, like getindex, but using variables.

var stylesheet = {
  "set": {
    "i": 1,
    "t": ["a","b","c"]
  },
  "getIndexVar": ["t", "i"]
};
// output : "b"
var stylesheet = {
  "set": {
    "i" : "b",
    "o" : { "a": 0, "b": 1, "c":2 },
  },
  "getPropertyVar": ["o", "i"]
};
// output : 1

Examples

See unit tests : https://github.com/Inist-CNRS/node-jbj-array/tree/master/test

Try it

http://Inist-CNRS.github.io/node-jbj/

License

MIT

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |