2.0.0 • Published 5 years ago
addidtoarray v2.0.0
addIdToArray
A simply function to add a id to your data.
const addIdToArray = require('addidtoarray');
addIdToArray([['Jeff', 19], ['Maria', 20], ],
  { headers: ['name', 'age']}
);
// --> [{id: 1, name: 'Jeff', age: 19}, ...]
addIdToArray({ name: 'Jeff', age: 19 });
// --> [{id: 1, name: 'Jeff', age: 19}]Install
Install with npm:
$ npm install addidtoarrayUsage
const addIdToArray = require('addidtoarray');Parameter
addIdToArray(arr, parameter: {headers, start, incrementName, 
  incrementStep, customIdFunction}
);| Parameter | Data type | Description | Example | Default | Required | 
|---|---|---|---|---|---|
| arr | Array or Object | Raw data without id. | 'Jeff',19 ;{'Jeff', 19}, ... | X | |
| parameter | Object | Optional parameters! | see in READMEor index.js | {} | |
| parameter.headers | String or String | How to call the propertiesof the object. | 'name', 'age' ;'name' | undefined | |
| parameter.start | Number | start + 1 is the first id. | 100 | 1 | |
| parameter.incrementName | String | How the 'id' property is called. | 'special_number' | 'id' | |
| parameter.incrementStep | Number | The increment step of the id. | 5 | 1 | |
| parameter.customIdFunction | Function(has to return an object) | Function to generate the id. | see in READMEor index.js | see in READMEor index.js | 
Custom id function
Requirements
Version: >= 1.2.0-develop
- has to accept two parameters
- item: Array or Object
 - params: Object
- current_number: calculated number with 
startandincrementStep - index: index from item in 
arr - incrementName: same as 
incrementNamefrom addIdToArray - incrementStep: same as 
incrementStepfrom addIdToArray - start: same as 
startfrom addIdToArray 
 - current_number: calculated number with 
 
 - has to return an object, if not --> use default function
 
Template
const customIdFunctionTemplate = (item, params) => {
  return {};
}Default
const simpleIdFunction = (item, params) => {
  const back = {};
  back[params.incrementName] = params.currentNumber;
  return back;
};Run tests
$ git clone https://github.com/LetsMelon/addIdToArray.git
$ cd addIdToArray
$ npm ci
$ npm testRun benchmarks
$ git clone https://github.com/LetsMelon/addIdToArray.git
$ cd addIdToArray
$ npm ci
$ npm run benchmarkExamples
knex.js
This library can be used to format demo data for a database.
Seed file:
const addIdToArray = require('addidtoarray');
exports.seed = async (knex) => {
  const countries = [
    ['USA', 'Washington D.C.'],
    ['Germany', 'Berlin'],
  ];
  const data = addIdToArray(countries, 
    { headers: ['name', 'capital']}
  );
  /*
   * data: [ { id: 1, name: 'USA', capital: 'Washington D.C.' },
   *         { id: 2, name: 'Germany', capital: 'Berlin' } ]
   */
  await knex('country').insert(data);
};custom Id function - hash
Require a 'hash' library like: object-hash, crypto-js, hash.js
const addIdToArray = require('addidtoarray');
const hash = require('object-hash');
const customHashIdFunction = (item, params) => {
  const back = {};
  back[params.incrementName] = hash(item);
  return back;
};
const data = [['Jeff', 19], ['Maria', 20]];
const hashedData = addIdToArray(data, 
  { headers: ['name', 'age'], customIdFunction: customHashIdFunction}
);
/*
 * hashedData: [{id: 'fb...75', name: 'Jeff', age: 19},
 *              {id: 'c2...82', name: 'Maria', age: 20}]
 */Todo
- better code documentation (maybe jsdoc)
 - add more examples
 - write better tests, see 1. issue
 - custom id function, see 2. issue
 
License
Copyright (C) 2020 Domenic Melcher
For more info see LICENSE