ticatec-enhanced-utils

0.1.2 • Public • Published

Enhanced Array Functions

This library provides two function for the array; the function replace can replace the first match element with the new item.
the function remove can remove the first match element from the array.

replace

arguments

item, the new data you would set into the array;
match, the 'match' should be a function, with returns true of false to determine whether the element matches the criteria or not. you can use the lambda function with one argument element.
return, if the array successfully replaces the matched element, the return value should be the index of this element, otherwise, -1 will be the value.

usage:

int array.replace(item, match);

example:

let arr = [
    {id:1, name:"aa"}, 
    {id:2, name:"bb"}, 
    {id: '234234', name:"第三个"}
  ];
let idx = arr.replace({id:2, name:"新元素"}, element => element.id === 2); 

remove

arguments

match, the 'match' should be a function, with returns true of false to determine whether the element matches the criteria or not. you can use the lambda function with one argument element.
return, if the array successfully replaces the matched element, the return value should be the index of this element, otherwise, -1 will be the value.

usage:

bool array.remove(match);

example:

let arr = [
    {id:1, name:"aa"}, 
    {id:2, name:"bb"}, 
    {id: '234234', name:"第三个"}
  ];
let success = arr.remove(element => element.id === 2); 

数组增强功能

为数组增加了两个方法,替换和删除

替换

从数组中找到地一个匹配的元素替换成新的元素,并返回替换元素的索引,如果没有匹配的,则返回-1,表示替换失败。

使用方法:

let arr = [
    {id:1, name:"aa"}, 
    {id:2, name:"bb"}, 
    {id: '234234', name:"第三个"}
  ];
let idx = arr.replace({id:2, name:"新元素"}, item => item.id === 2); 

删除

从数组中找到地一个匹配的元素替删除,如果没有匹配的,则返回false,表示删除失败。

使用方法:

let arr = [
    {id:1, name:"aa"}, 
    {id:2, name:"bb"}, 
    {id: '234234', name:"第三个"}
  ];
let success = arr.remove(item => item.id === 2); 

对象增强功能

增加空属性

  let s = {};
  s.addAttrs(['name', 'code', 'address']);

TicatecUtils

使用方法和函数列表:

     import ticatecUtils from 'ticatec-enhanced-utils';
     
     /**
     * 判断参数是不是一个函数
     * @param fun
     * @returns {boolean}
     */
    function isFunction (fun)
    
    /**
     * 调用函数
     * @param fun
     * @param params
     */
    function invokeFunction (fun, ...params)
    
    /**
     * 判断字符串是不是空
     * @param s
     * @returns {boolean}
     */
    function isEmpty (s) 
    
    /**
     * 解析url中的queryString
     * @param qs
     * @returns {Array}
     */
    function parseQueryString (qs)

Package Sidebar

Install

npm i ticatec-enhanced-utils

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

8.85 kB

Total Files

7

Last publish

Collaborators

  • henry.feng