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 with npm:
$ npm install addidtoarray
const addIdToArray = require('addidtoarray');
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 README or index.js |
{} | |
parameter.headers | String or [String] | How to call the properties of 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 README or index.js |
see in README or index.js |
Version: >= 1.2.0-develop
- has to accept two parameters
- item: Array or Object
- params: Object
- current_number: calculated number with
start
andincrementStep
- index: index from item in
arr
- incrementName: same as
incrementName
from addIdToArray - incrementStep: same as
incrementStep
from addIdToArray - start: same as
start
from addIdToArray
- current_number: calculated number with
- has to return an object, if not --> use default function
const customIdFunctionTemplate = (item, params) => {
return {};
}
const simpleIdFunction = (item, params) => {
const back = {};
back[params.incrementName] = params.currentNumber;
return back;
};
$ git clone https://github.com/LetsMelon/addIdToArray.git
$ cd addIdToArray
$ npm ci
$ npm test
$ git clone https://github.com/LetsMelon/addIdToArray.git
$ cd addIdToArray
$ npm ci
$ npm run benchmark
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);
};
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}]
*/
- better code documentation (maybe jsdoc)
- add more examples
- write better tests, see 1. issue
- custom id function, see 2. issue
Copyright (C) 2020 Domenic Melcher
For more info see LICENSE