npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

addidtoarray

v2.0.0

Published

Simple Function to add an id to an array (May change later!)

Downloads

6

Readme

addIdToArray

npm npm npm bundle size GitHub

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 addidtoarray

Usage

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 start and incrementStep
      • index: index from item in arr
      • incrementName: same as incrementName from addIdToArray
      • incrementStep: same as incrementStep from addIdToArray
      • start: same as start from addIdToArray
  • 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 test

Run benchmarks

$ git clone https://github.com/LetsMelon/addIdToArray.git
$ cd addIdToArray
$ npm ci
$ npm run benchmark

Examples

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
  • [x] custom id function, see 2. issue

License

Copyright (C) 2020 Domenic Melcher

For more info see LICENSE