2.0.3 • Published 3 years ago

black-bible v2.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

black-bible

一些常用的Javascript工具函数,可以简化我们日常的很多操作

Install

npm i black-bible -S

Usage

在Node环境下:

const _ = require('black-bible');

function f (a, b,c) {
    return a + b + c;
}

_.curry(f, 1)(2)(3); // 6

使用ES6模块:

import _ from 'black-bible';

function f (a, b,c) {
    return a + b + c;
}

_.curry(f, 1)(2)(3); // 6

使用script标签:

<script src="black-bible/umd"></script>
<script>

function f (a, b,c) {
    return a + b + c;
}

_.curry(f, 1)(2)(3); // 6
</script>

Modules

add ⇒ Number

求所有参数的和

对于参数列表中的任何类型,都会尝试相加,所以可能会有强制类型转换

ParamTypeDescription
...argsany参数

Example

// 'abc'
console.log(_.add('a', 'b', 'c'))

// 6
console.log(_.add(1, 2, 3))

after ⇒ function

函数在count次后执行

限制函数在触发多少次后执行

Returns: function - 执行器函数

ParamTypeDefaultDescription
fnfunction想要限制执行的函数
countNumber次数
pointanyglobalThis函数执行时环境,默认为globalThis

Example

function after (n) {
    console.log('只有在点击四次后才会执行我');
}


let button = document.querySelector('button');

let exec = _.after(after, 4);

button.addEventListener('click', exec);

before ⇒ function

限制函数执行的次数

限制函数只能执行多少次,返回一个执行器函数,用来执行函数,次数到之后,再次执行函数 会失效,

Returns: function - 执行器函数

ParamTypeDefaultDescription
fnfunction想要限制执行次数的函数
countNumber执行次数
pointanyglobalThis函数运行时所在环境,默认为globalThis

Example

function before (n) {
    console.log('只有在前四次点击才会执行我');
}


let button = document.querySelector('button');

let exec = _.before(before, 4);

button.addEventListener('click', exec);

camelCase ⇒ String

驼峰化字符串

函数会舍弃字符串的所有特殊字符,无论是在开头还是中间还是结尾,而在非开头处的数字或特殊字符 后的字母会大写

Returns: String - 驼峰化后的字符串

ParamTypeDescription
strString想要驼峰化的字符串

Example

let str1 = '@!$as3gh-at^e123!@#';
let str2 = 'a1b2c3';

camelCase(str1); // as3GhAtE123
camelCase(str2); // a1B2C3

checkedType ⇒ String

判断类型的函数

该函数通过Object.prototype.toString方法来判断,所以 若是重写了该方法,函数可能会失效,函数会返回对应类型的大写形式,

Returns: String - 返回一个值对应的类型的字符串

ParamTypeDescription
targetany一个需要检查类型的值

Example

_.checkedType({}); //  "Object"
 _.checkedType([]); // "Array"
 _.checkedType(true); // "Boolean"
 _.checkedType(undefined); // "Undefined"
 _.checkedType(null); // "Null"
 _.checkedType('str'); // "String"

compact ⇒ Array

获取真值数组

返回一个新数组,包含原数组的所有真值, 假值为:undefined, NaN, 0, false, null

Returns: Array - 数组中的真值元素组成的数组

ParamTypeDescription
arrArrayarray

Example

let arr = [0, 1, false, 2, null, '', 3, NaN, 4, undefined, 5];

// [1, 2, 3, 4, 5];
console.log( _.compact(arr) );

curry ⇒ function

函数柯里化的辅助函数

接受一个函数fn和可选的用于函数fn的参数,返回柯里化后的函数fn,即使函数fn的参数已经满足 同样会返回一个函数而不是fn调用的结果

Returns: function - 柯里化后的函数

ParamTypeDescription
fnfunction想要柯里化的函数
...argsany函数fn所需要的参数

Example

function add (a, b, c, d) {
     console.log(a + b + c + d);
 }

let curried = _.curry(add, 1, 1, 1, 1);

curried();  // 4

let curried2 = _.curry(add);

curried2(1)(1)(1)(1);  // 4

curried2(1, 1)(1)(1);  // 4

curried2(1, 1, 1)(1);  // 4

debounce ⇒ function

创建一个防抖函数

创建一个防抖函数,根据option选项对象来决定delay延时开始前或结束 后调用回调函数,其中option.before决定是否在延时开始之前触发回调,若是为false,那么 无论option.after为true还是false,都会在延时结束后触发回调,option.after决定是否 在延时结束之后触发回调,函数返回的防抖函数拥有一个cancel方法,用来取消延时调用

Returns: function - debounced 防抖函数

ParamTypeDescription
cbfunction回调函数
delay0延时
optionObject选项对象
option.beforeBoolean是否在延时开始之前触发回调
option.afterBoolean是否在延时结束之后触发回调

Example

function go () {
        console.log('scroll');
}

window.addEventListener( 'scroll', _.debounce(go, 1000) );

window.addEventListener( 'scroll', _.debounce(go, 1000, {before: true, after: true}) );

deepClone ⇒ any

深拷贝函数

一个深拷贝函数,cloneProto参数决定是否拷贝一个对象的原型,不会忽略函数,对象的 Symbol属性,但是对于Symbol属性,仍然传递的是Symbol的引用

Returns: any - 深拷贝后的对象

ParamTypeDescription
targetany想要进行深拷贝的对象
cloneProtoBoolean是否拷贝对象的原型

Example

let obj = {
     name: 'Jack',
     age: 30
};

let o2 = _.deepClone(obj);

delay ⇒ Object

延时函数

延时函数fn的执行wait毫秒,会返回一个定时器ID

Returns: Object - 定时器ID

ParamTypeDefaultDescription
fnfunction想要延时的函数
waitNumber延时
argsArray函数fn需要的参数,需要放到一个数组里面
pointanyglobalThis函数fn执行时环境,默认为globalThis

Example

let obj = {
     name: 'Sun XiaoChuan',
     age: 6,
     introduction() {
     console.log(`My name is ${this.name} I'm ${this.age} years old`)
     }
}

// My name is sun Xiaochuan. I'm 6 years old
let timerId = delay(obj.introduction, 1000, [], obj);

divide ⇒ Boolean

求商

ParamTypeDescription
collectionArray | Object | Map | Set集合

Example

// 2
console.log(_.divide([4, 2]));

divideBy ⇒ Number

求商

接受一个函数作为第二参数,对集合内每个元素调用,用来决定求值的标准

ParamTypeDescription
collectionArray | Object | Map | Set集合
iteratorfunction迭代函数

Example

let arr = [
{age: 30},
{name: 'xxx'},
{age: 30}
];

let arrf = (o) => o.age;


// 1
console.log(_.divideBy(arr , arrf))

every ⇒ Boolean

every迭代函数

适用于多种类型的every函数,对于对象的每个元素调用函数,若所有属性都使 函数返回True则返回True,否则返回false

Returns: Boolean - 是否每个元素都对函数返回True

ParamTypeDescription
targetArray | Object | Map | Setobject
predicatefunction断言函数

Example

let arr = [1, 2, 3];

console.log( _.every(arr, item => item > 0) );  // true

filter ⇒ Array

适用于各种类型的Filter迭代函数

适用于各种类型的Filter迭代函数,对于集合的每一项调用函数,返回函数返回True的 项组成的数组

Returns: Array - 函数返回true的项组成的数组

ParamTypeDescription
targetArray | Map | Set | String | Number对象
cbfunction对对象的元素调用的函数

Example

_.filter([1, 2, 3, 4], item => item % 2 === 0);   // [2, 4]

_.filter('Jack', char => true);  // ['J', 'a', 'c', 'k']

_.filter(5, n => true);   // [0, 1, 2, 3, 4];

find ⇒ any

find迭代函数

适用于多种类型的find方法,它返回第一个使断言函数返回true的项, 对于Object和Map类型,find返回的将不再是项,而是键值对中的键, fromIndex为迭代的起点,对于数组来说,是其索引,对于对象Map等类型来说, 是获取他们的所有键的数组的索引

Returns: any - 第一个使断言函数返回true的项

ParamTypeDescription
targetArray | Map | Set | Objectobject
predicatefunction断言函数,
fromIndexNumber迭代的起始索引

Example

let arr = [1, 2, 3, 4, 5, 6];

console.log( _.find(arr, item => item > 3) );  // 4

let obj = {a: 1, b: 2, c: 3};

console.log(  _.find(obj, item => item > 2) );  // c

findRight ⇒ any

find函数的反向函数

从反方向开始迭代,返回第一个使函数返回True的项的值 对于对象或Map,返回的将是他们的键而不是值

Returns: any - 第一个使断言函数返回true的项

ParamTypeDescription
targetArray | Object | Map | Setobject
fnfunction对象每一项调用的函数
fromIndexNumber迭代的起始索引

Example

let arr = [1, 2, 3, 4];

console.log( _.findRight(arr, item => item > 1) ); // 4

forEach ⇒ Array | Map | Set | String | Number | Object

forEach迭代函数

适用于各种类型的forEach,对集合的每一项运行函数,最终返回集合本身, 可以显式的通过返回false来终止循环

Returns: Array | Map | Set | String | Number | Object - 集合本身

ParamTypeDescription
targetArray | Map | Set | String | Number | Object对象
cbfunction对对象的元素调用的函数

Example

_.forEach([1, 2, 3], item => console.log(item)); 1, 2, 3

_.forEach({name: 'Jack', age: 30}, value => console.log(value)); // 'Jack', 30

forEachRight ⇒ Array | Object | Map | Set

forEachRight迭代函数

forEach函数的反向函数,从结尾开始迭代,可以通过返回false显式的 结束迭代,返回集合本身

ParamTypeDescription
targetArray | Object | Map | Set想要迭代的集合
fnfunction对每个元素调用的迭代函数

Example

let arr = [1, 2, 3, 4];

 _.forEachRight(arr, item => console.log(item));   // 4, 3, 2, 1

isArray

是否是数组类型

使用Array的isArray方法来进行检查

ParamTypeDescription
valueany要检查的值

Example

// true
_.isArray([]);

isBigInt ⇒ Boolean

值是否是BigInt类型

ParamTypeDescription
value*需要检查的值

Example

let bigInt1 = 123n,
     bigInt2 = BigInt(456),
     bigInt3 = Object(789n);

// true
console.log( isBigInt(bigInt1) );
// true
console.log( isBigInt(bigInt2) );
// true
console.log( isBigInt(bigInt3) );

isBoolean ⇒ Boolean

检查是否是布尔值

ParamTypeDescription
value*需要检查的值

Example

// true
_.isBoolean(true)


// true
_.isBoolean(false)

isDate ⇒ Boolean

值是否是Date

ParamTypeDescription
value*想要检查的值

isError ⇒ Boolean

是否是Error类型

ParamTypeDescription
value*想要检查的值

Example

// true
console.log(_.isError(new Error()))

isFalseValue ⇒ Boolean

检查是否是假植

在这里,假值的概念为值等于 undefined ,null ,false ,0

ParamTypeDescription
value*想要检查的值

Example

// false
console.log( _.isFalseValue(0) );
console.log( _.isFalseValue(false) );
console.log( _.isFalseValue(null) );
console.log( _.isFalseValue(undefined) );

isFinite ⇒ Boolean

值是否是有限数字

ParamTypeDescription
value*需要检查的值

Example

// true
console.log( _.isFinite(100) );

// false
console.log( _.isFinite(1e10000) );

isFunction ⇒ Boolean

值是否是函数

ParamTypeDescription
value*想要检查的值

Example

// true
console.log(_.isFunction(function f(){}))

isInContainer ⇒ Boolean

判断一个元素是否在对应容器的可视区域内

Returns: Boolean - 是否在可视区域内

ParamTypeDescription
elObject判断是否在容器的目标DOM元素
containerObject容器DOM元素,默认为视口

isMap ⇒ Boolean

检查值是否为Map

ParamTypeDescription
value*需要检查的值

Example

// true
console.log(_.isMap(new Map()))

isNaN ⇒ Boolean

检查是否是NaN

与Javascript自带的isNaN不同,该函数只会对NaN值返回true

ParamTypeDescription
value*想要检查的值

Example

// true
_.isNaN(NaN);

// false
_.isNaN('AAA');

isNodeEnv ⇒ Boolean

是否是Node环境

isNull ⇒ Boolean

检查值是否等于Null

ParamTypeDescription
value*需要检查的值

Example

// true
console.log(_.isNull(null));

isNumber ⇒ Boolean

检查是否是数字

ParamTypeDescription
value*想要检查的值

Example

// true
_.isNumber(new Number(100));

// true
_.isNumber(100);

isObject ⇒ Boolean

检查是否是对象类型

对于任何对象类型包括函数返回True,对于null返回false和undefined返回false

ParamTypeDescription
valueany想要检查的值

Example

// true
_.isObject([])

// false
_.isObject(null)

// false
_.isObject(undefined)

isObjectLike ⇒ Boolean

检查是否是类对象

跟isObject基本相同,只是会对函数返回false,也就是说对 一个非null非function对象返回True

ParamTypeDescription
value*想要检查的值

Example

// false
_.isObjectLike(function f () {})

isPlainObject ⇒ Boolean

检查值是否为普通对象

一个普通对象为直接通过Object构造函数或对象字面量创建的对象, 一个对象若是继承了其他对象也算是普通对象

ParamTypeDescription
value*需要检查的值

Example

let o1 = {},
     o2 = {};

Object.setPrototypeOf(o1, o2);


// true
console.log(_.isPlainObject(o1))


// true
console.log(_.isPlainObject(o2))

isRegExp ⇒ Boolean

是否是RegExp

ParamTypeDescription
value*检查的值

Example

// true
console.log(_.isRegExp(new RegExp()))

isSet ⇒ Boolean

是否是Set

ParamTypeDescription
value*需要检查的值

Example

// true
console.log(_.isSet(new Set()))

isString ⇒ Boolean

检查是否是字符串

ParamTypeDescription
value*想要检查的值

Example

// true
_.isString('aaa')

// true
_.isString(new String())

isSymbol ⇒ Boolean

检查值是否是Symbol

ParamTypeDescription
value*需要检查的值

Example

// true
console.log(_.isSymbol( Symbol("foo")))

// true
console.log(_.isSymbol(Object(Symbol("foo"))))

isTrueValue ⇒ Boolean

值是否是真值

在这里,真值的概念为不等于 undefined ,null ,0 ,false,

ParamTypeDescription
value*想要检查的值

Example

// false
console.log(_.isTrueValue(undefined));

// false
console.log(_.isTrueValue(0));

// false
console.log(_.isTrueValue(null));

// false
console.log(_.isTrueValue(false));

// true
console.log(_.isTrueValue('0'));

isUndefined ⇒ Boolean

检查值是否是Undefined

ParamTypeDescription
value*需要检查的值

Example

// true
console.log( _.isUndefined(Undefined) );

isWeakMap ⇒ Boolean

检查值是否是WeakMap

ParamTypeDescription
value*想要检查的值

Example

// true
console.log(_.isWeakMap(new WeakMap()))

isWeakSet ⇒ Boolean

检查值是否是WeakSet

ParamTypeDescription
value*想要检查的值

Example

// true
console.log(_.isWeakSet(new WeakSet()))

isWindow ⇒ Boolean

检查当前宿主环境是否为window

map ⇒ Array

map迭代函数

适用于各种类型的map迭代函数,对集合的每一项调用函数fn,返回函数 的返回值组成的数组

Returns: Array - 函数的返回值组成的数组

ParamTypeDescription
targetArray | Map | Set | String | Numberobj
fnfunction迭代函数

Example

_.map(5, item => item + '号'); // [ '0号', '1号', '2号', '3号', '4号' ]

_.map([1, 2, 3, 4, 5], (item, index) => index + '号'); // [ '0号', '1号', '2号', '3号', '4号' ]

max ⇒ Number | String

集合的最大值

ParamTypeDescription
collectionArray | Object | Map | Set集合

Example

// 3
console.log(_.max([1, 2, 3]))

// c
console.log(_.max({'a', 'b', 'c'}))

maxBy ⇒ any

最大值

接受一个函数作为第二参数,用来决定求值的标准

ParamTypeDescription
collectionArray | Object | Map | Set集合
iteratorfunction迭代函数

Example

let arr = [
{age: 30},
{name: 'xxx'},
{age: 20}
];

let arrf = (o) => o.age;


// {age: 30}
console.log(_.maxBy(arr, arrf));

min ⇒ Number | String

求最小值

ParamTypeDescription
collectionArray | Map | Set | Object集合

Example

// 0
console.log(_.min([1, 2, 3, 0]))

minBy ⇒ any

最小值

接受一个函数作为第二参数,用来决定求值的标准

ParamTypeDescription
collectionArray | Object | Map | Set集合
iteratorfunction迭代函数

Example

let arr = [
{age: 30},
{name: 'xxx'},
{age: 20}
];

let arrf = (o) => o.age;

// {age: 20}
console.log(_.minBy(arr, arrf));

multiply ⇒ Number

求集合的乘积

ParamTypeDescription
collectionArray | Object | Map | Set集合

Example

// 24
console.log(_.multiply([2, 3, 4]))

multiplyBy ⇒ Number

集合的乘积

接受一个函数作为第二参数,用来决定求值的标准

ParamTypeDescription
collectionArray | Map | Set | Object集合
iteratorfunction迭代函数

Example

let arr = [
{age: 30},
{name: 'xxx'},
{age: 20}
];
let arrf = (o) => o.age;

// 600
console.log(_.multiplyBy(arr, arrf))

pull ⇒ Array

移除数组元素

删除数组中所有与给定值相等的元素,给定值可以有多个,该方法会在原数组修改, 返回修改完成后的数组

Returns: Array - 修改完成的数组

ParamTypeDescription
arrArrayarray
...argsany想要删除的元素

Example

let arr = [1, 2, 3, 4, 3, 2, 6, 1, 2];

_.pull(arr, 1, 2);  //  [3, 4, 3, 6]

random ⇒ Number

随机数函数

返回min-max之间的随机数,包括min和max,默认返回整数

Returns: Number - 一个数字

ParamTypeDescription
minNumber最小值
maxNumber最大值
floatingBoolean是否包括浮点数

Example

// 2, 5, 1, 1, 2
_.forEach(5, () => {
console.log( _.random(1, 5) );
});

reduce ⇒ any

reduce归并函数

适用于多种类型的归并函数,函数接受四个参数, 前一项的值,当前项的值,当前项的索引,对象本身,函数的返回值会作为第一个 参数自动传给下一项,也可以通过使函数返回false终止迭代

Returns: any - 函数fn最终的返回值

ParamTypeDescription
targetArray | String | Object | Map | Setobj
fnfunction对目标每一项调用的函数
accumulatorany初始值

Example

let o = {a: 1, b: 2, c: 3};

// 6
let result = _reduce(o, (cur, pre) => {
     return pre + cur;
});

reduceRight ⇒ any

rediceRight归并函数

reduce函数的反向函数,从反方向开始迭代,对每一项调用函数fn, 函数fn接受4个参数,前一项的值,当前项的值,当前项的索引,对象本身, 若accumulator存在,则从第一项开始迭代,否则,从第二项开始迭代 若fn函数显式的返回false,则终止迭代,返回结果

Returns: any - 构建的最终值

ParamTypeDescription
targetArray | Object | Map | Setobject
fnfunction对对象每个元素调用的函数
accumulatorany初始值

Example

let arr = [0, 1, 2, 3, 4];

console.log( _.reduceRight(arr, (pre, cur) => pre + cur) );  // 10

remove ⇒ Array

删除数组元素

删除数组中所有对函数返回True的项,返回删除的项,在原数组删除 断言函数接受三个参数,数组当前项的值,索引和数组本身

Returns: Array - 函数返回true的项组成的数组

ParamTypeDescription
arrArrayarray
fnfunction断言函数,

Example

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

_.remove(arr);    //  [ 2, 4, 6, 8, 10 ]

console.log(arr);  //  [ 1, 3, 5, 7, 9 ]

sample ⇒ any

随机获取集合的某一项的值

随机获取集合的某一项,对于Object和Map类型来说,获取得将会是他们的 Key,而不是Value

Returns: any - 集合的某一项

ParamTypeDescription
targetArray | Object | Map | Set | String一个集合

Example

let arr = [1, 2, 3, 4];

// 3, 2, 2, 3, 3, 1, 2, 4, 4, 1

_.forEach(10, () => {
     console.log( _.sample(arr) )
})

sampleSize ⇒ Array

随机抽取一个集合的num个元素

随机抽取一个集合的num个元素,适用于多种类型,若是num大于集合内元素的数量,会随机抽取 集合所有的元素,会将抽取到的元素放到一个数组内,

Returns: Array - 抽取的元素组成的数组

ParamTypeDescription
targetArray | Map | Set | Object | String想要随机抽取的对象
numNumber想要获得的元素数量

Example

let arr = [1, 2, 3, 4];

// [3, 1]
console.log(_.sampleSize(arr, 2));

// [2, 4, 3, 1]
console.log(_.sampleSize(arr, 100));

shuffle ⇒ Array | Object | Map | Set | String

打乱集合的顺序

打乱一个集合的顺序,不会改变原集合,而是返回一个新集合,

Returns: Array | Object | Map | Set | String - 打乱顺序后的集合

ParamTypeDescription
targetArray | Object | Map | Set | String想要打乱顺序的集合

Example

let arr = [1, 2, 3, 4];

// [2, 4, 3, 1]
console.log(_.shuffle(arr));

let str = 'ABCD';

// 'ADBC';
console.log(_.shuffle(str));

size ⇒ Number

一个集合内元素的数量

对于数组和字符串,为他们的length属性,其他类型的对象,为对象内所有 的可迭代属性的数量

Returns: Number - 集合内元素的数量

ParamTypeDescription
targetArray | String | Object | Map | Set一个集合

Example

let o = {name: 'Davi', age: 22};

// 2
console.log(_.size(o));

some ⇒ Boolean

some迭代函数

适用于多种类型的Some函数,对对象的每一项调用函数,若是有一项使函数返回True, 则返回True,否则返回false

ParamTypeDescription
targetArray | Object | Map | Set对象
predicatefunction断言函数

Example

let arr = [1, 2, 3, 4];

console.log( _.some(arr, item => item > 3) );  // true

subtract ⇒ Number

集合内所有元素的差

ParamTypeDescription
collectionArray | Object | Map | Set集合

Example

// 0
console.log(_.subtract([3, 2, 1]))

subtractBy ⇒ Number

集合内元素的差

接受一个函数作为第二参数,对集合内每个元素调用,用来决定求值的标准

ParamTypeDescription
collectionArray | Object | Map | Set集合
iteratorfunction迭代函数

Example

let obj = {
name: [1, 2, 3],
age: [4, 5, 6],
color: [7, 8, 9]
};

let objf = (o) => o[0];

// -10
console.log(_.subtractBy(obj, objf));



let arr = [
{age: 30},
{name: 'xxx'},
{age: 20}
];

let arrf = (o) => o.age;


// 10
console.log(_.subtractBy(arr, arrf));

sum ⇒ Number

集合内所有值的和

ParamTypeDescription
collectionArray | Object | Map | Set集合

Example

// 6
console.log(_.sum([1, 2, 3]))

sumBy ⇒ Number

集合内元素的和

接受一个函数作为第二参数,用来决定求值的标准

ParamTypeDescription
collectionArray | Object | Map | Set一个集合
iteratorfunction用来迭代的函数

Example

let arr = [
{age: 30},
{name: 'xxx'},
{age: 20}
];

let arrf = (o) => o.age;

// 50
console.log(_.sumBy(arr, arrf))

swapIndex ⇒ Array | String

交换元素位置

交换数组或字符串元素的位置,对于数组,在数组本身修改,对于字符串,会返回新的字符串

ParamTypeDescription
targetArray | String数组或字符串
iNumber索引
jNumber索引

Example

_.swapIndex([1, 2], 0, 1); // [2, 1]

_.swapIndex('abcd', 0, 3); // dbca

throttle ⇒ function

节流函数

Returns: function - 节流后的函数

ParamTypeDescription
fnfunction事件处理函数
timeNumer延时

Example

function go () {
        console.log('scroll');
}

window.addEventListener( 'scroll', _.throttle(go, 1000) );

toCurrency ⇒ String

将一个数字转化为以逗号分隔的货币形式的字符串

Returns: String - 货币形式的字符串

ParamTypeDescription
numNumber一个数字

Example

//  123,456,789.123456
console.log( toCurrency(123456789.123456) );

toLower ⇒ any

将一个字符串或集合中的字符串转化为小写形式

会递归一个集合所有的深度,将它们的字符串值全部转化

ParamTypeDescription
targetString | Object | Map | Array想要小写的字符串或集合

Example

let arr = ['AAA', ['BBB', ['CCC']]];

//  ['aaa', ['bbb', ['ccc']]];
console.log(_.toLower(arr));

toUpper ⇒ String | Array | Object | Map

将字符串或一个集合中的字符串大写,

该方法会递归一个集合的所有深度,将集合的所有字符串转化为大写形式

Returns: String | Array | Object | Map - 转化完成的字符串或集合

ParamTypeDescription
targetString | Array | Object | Map想要大写的字符串或集合

Example

let arr = ['aaa', {name: 'davi'}, 20];

// ['AAA', {name: 'DAVI'}, 20]
console.log(_.toUpper(['aaa', { name: 'davi' }, 20]));

uniq ⇒ Array

数组去重

在原数组修改,返回修改后的数组

Returns: Array - 去重后的数组

ParamTypeDescription
arrArrayarray

Example

let arr = [1, 2, 1, 3, 2, 4];

_.uniq(arr);

// 1, 2, 3, 4
console.log(arr);

uniqBy ⇒ Array

数组去重

接受一个函数作为第二参数,用来决定去重的条件,在原数组去重

Returns: Array - 去重后的数组

ParamTypeDescription
arrArrayarray
iteratorfunction迭代函数

Example

let arr = [
{age: 30, name: 'a'},
{age: 20, name: 'b'},
{age: 30, name: 'c'},
{age: 10, name: 'd'},
{age: 90, name: 'e'},
{age: 10, name: 'f'},
];

_.uniqBy(arr, item => item.age);

console.log(arr);

unique ⇒ Array

去重函数

数组去重,不改变原数组,返回去重后的新数组

Returns: Array - 去重后的数组

ParamTypeDescription
arrArrayarray

Example

let arr = [1, 2, 3, 4, 1, 2, 3, 5];

_.unique(arr);  // [1, 2, 3, 4, 5]

upperFirst ⇒ String | Array | Object | Map

将字符串或一个集合中的字符串首字母大写,

该方法会递归一个集合的所有深度,将集合的所有字符串转化为首字母大写形式

Returns: String | Array | Object | Map - 转化完成的字符串或集合

ParamTypeDescription
targetString | Array | Object | Map想要首字母大写的字符串或集合

Example

let arr = ['aaa', {name: 'davi'}, 20];

// ['Aaa', {name: 'Davi'}, 20];
console.log(_.upperFirst(arr));

withOut ⇒ Array

不改变原数组,返回一个去除给定值的新数组

返回一个新数组,其中包含所有不等于value的值

ParamTypeDescription
arrArrayarray
...valuesany想要移除的值

Example

let arr = [1, 1, 0, 2, 3 ,4 ,5 ,3, 6];

// [0, 2, 4, 5, 6]
console.log( _.withOut(arr, 1, 3) );
2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.0.11

3 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago