@12luckydev/utils

Utils for working with arrays!

Usage no npm install needed!

<script type="module">
  import 12luckydevUtils from 'https://cdn.skypack.dev/@12luckydev/utils';
</script>

README

@12luckydev/utils

npm (scoped) npm bundle size (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]

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"}
]
**/

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"}
]
**/

removeAt

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

removeAt(['a', 'b', 'c'], 1); // result ["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"}
]
**/

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

License

MIT © 12LuckyDev