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

@12luckydev/utils

v2.0.0

Published

Utils for working with arrays!

Downloads

162

Readme

@12luckydev/utils

npm (scoped)

Few helper funtions to work with arrays without mutation. Helpful with managing arrays in react.js state.

Install

# using npm
npm i @12luckydev/utils

# using yarn
yarn add @12luckydev/utils

Functions

merge

import { merge } from '@12luckydev/utils';

merge([1, 2, 3], [4, 5]); // result: [1,2,3,4,5]

add

import { add } from '@12luckydev/utils';

add([1, 2, 3], 4, 5); // result: [1,2,3,4,5]

isInRange

import { isInRange } from '@12luckydev/utils';

const array = [0, 1, 2, 3, 4, 5, 6, 7];
isInRange(array, 5); // result: true

compare

import { compare } from '@12luckydev/utils';

const array = [0, 1, 2, 3, 4, 5];
compare(array, [0, 1, 2, 3, 4, 5]); // result: true

const objArray = [
    { id: 1, value: 'value 1' },
    { id: 2, value: 'value 2' },
    { id: 3, value: 'value 3' },
  ];

compare(
      objArray,
      [
        { id: 2, value: 'value 2' },
        { id: 3, value: 'value 3' },
        { id: 4, value: 'value 4' },
      ],
      (elA, elB) => elA.id === elB.id,
    ); // result: true

move, moveUp, moveDown

import { move, moveUp, moveDown } from '@12luckydev/utils';

const array = ['a', 'b', 'c', 'd', 'e'];

move(array, 3, 1); // result: ['a', 'd', 'b', 'c', 'e']

moveUp(array, 2); // result: ['a', 'b', 'd', 'c', 'e']

moveDown(array, 2); // result: ['a', 'c', 'b', 'd', 'e']

shift

import { shift } from '@12luckydev/utils';

const array = ['a', 'b', 'c', 'd', 'e'];

shift(array, 1); // result: ['e', 'a', 'b', 'c', 'd']

shift(array, -1); // result: ['b', 'c', 'd', 'e', 'a']

shift(array, 1, false); // result: ['a', 'b', 'c', 'd']

shift(array, -1, false); // result: ['b', 'c', 'd', 'e']

editAt

import { editAt } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

editAt(input, { id: 4, name: 'Ann' }, 1);
/**
    result: [
        {id: 1, name: "Kevin"},
        {id: 4, name: "Ann"},
        {id: 3, name: "Bob"}
]
**/

editPropAt

import { editPropAt } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

editPropAt(input, 'name', 'Angela', 2)
/**
    result: [
        { id: 1, name: 'Kevin' },
        { id: 2, name: 'Karen' },
        { id: 3, name: 'Angela' }
]
**/

editByProp

import { editByProp } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

editByProp(input, { id: 4, name: 'Ann' }, 'id', 2)
/**
    result: [
        {id: 1, name: "Kevin"},
        {id: 4, name: "Ann"},
        {id: 3, name: "Bob"}
]
**/

remove

import { remove } from '@12luckydev/utils';

remove(['a', 'b', 'c'], 'b'); // result ["a", "c"]

toggle

import { toggle } from '@12luckydev/utils';

toggle(['a', 'b', 'c'], 'b'); // result ["a", "c"]

toggle(['a', 'c'], 'b'); // result ['a', 'b', 'c']

removeByProp

import { toggleByProp } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

toggleByProp(input, 'name', { id: 2, name: 'Karen' });
/**
    result: [
        {id: 1, name: "Kevin"},
        {id: 3, name: "Bob"}
]
**/

toggleByProp(input, 'name', { id: 4, name: 'Iwan' });
/**
    result: [
       { id: 1, name: 'Kevin' },
       { id: 2, name: 'Karen' },
       { id: 3, name: 'Bob' },
       { id: 4, name: 'Iwan' }
]
**/

removeAt

import { removeAt } from '@12luckydev/utils';

removeAt(['a', 'b', 'c'], 1); // result ["a", "c"]

popAt

import { popAt } from '@12luckydev/utils';

popAt(['a', 'b', 'c'], 1); // result ["b", ["a", "c"]]

removeByProp

import { removeByProp } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

removeByProp(input, 'name', 'Karen');
/**
    result: [
        {id: 1, name: "Kevin"},
        {id: 3, name: "Bob"}
]
**/

popByProp

import { popByProp } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

popByProp(input, 'name', 'Karen');
/**
    result: [
      { id: 2, name: 'Karen' },
      [
        { id: 1, name: "Kevin" },
        { id: 3, name: "Bob" }
      ]
  ]
**/

popByProp(input, 'name', 'Karen', false);
/**
    result: [
      [{ id: 2, name: 'Karen' }],
      [
        { id: 1, name: "Kevin" },
        { id: 3, name: "Bob" }
      ]
  ]
**/

removeByProps

import { removeByProps } from '@12luckydev/utils';

const input = [
  { id: 1, name: 'Kevin' },
  { id: 2, name: 'Karen' },
  { id: 3, name: 'Bob' },
];

removeByProp(input, 'name', ['Karen', 'Bob']);
/**
    result: [{id: 1, name: "Kevin"}]
**/

nMap

import { nMap } from '@12luckydev/utils';

nMap(3, (i) => `${i} value`); //result: ["0 value", "1 value", "2 value"]

isObject

import { isObject } from '@12luckydev/utils';

isObject(null); //result: true
isObject({}); //result: true
isObject(null, false); //result: false
isObject({}, false); //result: true
isObject([]); //result: false - arrays are excluded

isArray

import { isArray } from '@12luckydev/utils';

isArray(null); //result: false
isArray({}); //result: false
isArray([]); //result: true
isArray(["a", "b"]); //result: true
isArray([], false); //result: false
isArray(["a", "b"], false); //result: true

isFunc

import { isFunc } from '@12luckydev/utils';

isFunc(() => console.log("Hello")); //result: true
isFunc({}); //result: false

mapToObject

import { mapToObject } from '@12luckydev/utils';

const inputArray = [
    { id: 1, value: 'a' },
    { id: 2, value: 'b' },
    { id: 3, value: 'c' },
  ];

mapToObject(inputArray, 'value')
/**
  result : {
    a: { id: 1 },
    b: { id: 2 },
    c: { id: 3 },
  }
 */

mapToObjectUsing

import { mapToObjectUsing } from '@12luckydev/utils';

const inputArray = [
    { id: 1, value: 'a' },
    { id: 2, value: 'b' },
    { id: 3, value: 'c' },
  ];

mapToObjectUsing(inputArray, 'value', (v) => {
    return v.value;
});
/**
  result : {
    a: 'a',
    b: 'b',
    c: 'c',
  }
 */

forEachProp

import { forEachProp } from '@12luckydev/utils';
const input = { a: 1, b: 2, c: 3 };

forEachProp(input, (v) => console.log(v));
// result (console): 1, 2, 3

License

MIT © 12LuckyDev