@12luckydev/array-handler 中文文档教程

发布于 4年前 浏览 37 项目主页 更新于 3年前

@12luckydev/array-handler

npm (scoped)

用于处理数组操作的包装函数,没有突变

使用 @12luckydev/ utils

Install

# using npm
npm i @12luckydev/@12luckydev/array-handler

# using yarn
yarn add 12luckydev/@12luckydev/array-handler

Usage examples

import arrayHandler, { ARRAY_OPERATION } from '@12luckydev/array-handler';

const simpleInput = [1, 2, 3];

//ADD
const addResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.ADD, { newValue: 4 });

//result: [1, 2, 3, 4]


//EDIT_AT
const editAtResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.EDIT_AT, { newValue: 4, index: 1 });

//result: [1, 4, 3]


//REMOVE_AT
const removeAtResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.REMOVE_AT, { index: 1 });

//result: [1, 3]


//MOVE_UP
const moveUpResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.MOVE_UP, { index: 1 });

//result: [1, 3, 2]


//MOVE_DOWN
const moveUpResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.MOVE_DOWN, { index: 1 });

//result: [2, 1, 3]


//EDIT_PROP_AT

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

const editPropAtResultArray =
    arrayHandler(objectInput, ARRAY_OPERATION.EDIT_PROP_AT, { index: 1, key: 'id', propValue: 4 });

/** result:
 *  [{ id: 1, name: 'a' },
 *   { id: 4, name: 'b' },
 *   { id: 3, name: 'c' }];
 */

/**
 * changeHandler
 * Add a function as the 4th argument if you want to edit the entire array after performing the operation
*/

const changeHandler = ({
  operation,
  newArray,
  oldArray,
  newValue,
  index,
  key,
  propValue,
}) => {
  return newArray.map((el) => el * 2);
}

const changeHandlerResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.ADD, { newValue: 4 }, changeHandler);

//result: [2, 4, 6, 8]

License

麻省理工学院 © 12LuckyDev

@12luckydev/array-handler

npm (scoped)

Wrapped functions for handle operations on array without mutation

Using @12luckydev/utils

Install

# using npm
npm i @12luckydev/@12luckydev/array-handler

# using yarn
yarn add 12luckydev/@12luckydev/array-handler

Usage examples

import arrayHandler, { ARRAY_OPERATION } from '@12luckydev/array-handler';

const simpleInput = [1, 2, 3];

//ADD
const addResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.ADD, { newValue: 4 });

//result: [1, 2, 3, 4]


//EDIT_AT
const editAtResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.EDIT_AT, { newValue: 4, index: 1 });

//result: [1, 4, 3]


//REMOVE_AT
const removeAtResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.REMOVE_AT, { index: 1 });

//result: [1, 3]


//MOVE_UP
const moveUpResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.MOVE_UP, { index: 1 });

//result: [1, 3, 2]


//MOVE_DOWN
const moveUpResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.MOVE_DOWN, { index: 1 });

//result: [2, 1, 3]


//EDIT_PROP_AT

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

const editPropAtResultArray =
    arrayHandler(objectInput, ARRAY_OPERATION.EDIT_PROP_AT, { index: 1, key: 'id', propValue: 4 });

/** result:
 *  [{ id: 1, name: 'a' },
 *   { id: 4, name: 'b' },
 *   { id: 3, name: 'c' }];
 */

/**
 * changeHandler
 * Add a function as the 4th argument if you want to edit the entire array after performing the operation
*/

const changeHandler = ({
  operation,
  newArray,
  oldArray,
  newValue,
  index,
  key,
  propValue,
}) => {
  return newArray.map((el) => el * 2);
}

const changeHandlerResultArray = arrayHandler(simpleInput, ARRAY_OPERATION.ADD, { newValue: 4 }, changeHandler);

//result: [2, 4, 6, 8]

License

MIT © 12LuckyDev

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文