viperjs
常用js片段组成的工具库,代码是es6编写,可以拿出单条片段进行babel再使用。
获取
# npm npm install viperjs
目录
ARRAY
点击查看
arrayConcat
difference
includes
intersection
remove
sample
samplesize
chunk
compact
countoccurrences
deepFlatten
flatten
foreachright
distinctvaluesofarray
dropright
everynth
filternonunique
indexofall
initial
initializearraywithrange
initializearraywithvalues
issorted
join
last
longestitem
maxn
minn
nthelement
partition
pull
pullatindex
pullatvalue
reducedfilter
shuffle
similarity
symmetricdifference
tail
take
takeright
union
without
zip
zipobject
average
BROWSER
FUNCTION
Math
OBJECT
OBJECT
点击查看
Type
点击查看
UTILITY
点击查看
coalesce
coalescefactory
extendhex
geturlparams
hextorgb
randomhexcode
rgbtohex
timetaken
uuidgeneratorbrowser
validemail
ARRAY
ArrayConcat
数组拼接
viper ;
例子
viper // [1, 2, 3, [4]]
difference
数组比较 过滤出数组 a中数组b不包含的值
viper { const s = b return a }
例子
viper // [3]
includes
数组|字符串包含
viper collection != -1
例子
viper // trueviper // false
intersection
数组交集 返回两数组中同时包含的值
viper { const s = b; return a}
例子
viper // [2,3]
remove
根据函数移除数组中的元素并返回移除元素
viper Array ? arr :
例子
viper // [2, 4]
sample
随机获取数组或字符串中一个元素
viper arrMath
例子
viper // 2
sampleSize
从数组中获取n个随机元素,最大为数组的大小
viper { let m = arrlength; while m const i = Math; arrm arri = arri arrm; return arr;};
例子
viper; // [1,3]viper; // [1,3,2]
chunk
chunk分割成指定大小的数组, 如果不能整分,剩余的会组成一个新的数组
viper Array
例子
viper; // [[1,2],[3,4],[5]]
compact
删除数组中错误的值 false, null, 0, “”, undefined, and NaN
viper arr
例子
vipercompact1 false NaN // [1]
countOccurrences
统计数组中某个值出现的次数
viper arr;
例子
viper; // 3
deepFlatten
深度平铺数组 转换为一维数组
viper
例子
viper; // [1,2,3,4,5]
flatten
创建指定唯度的数组 默认为1
viper depth != 1 ? arr : arr
例子
viper; // [1, 2, 3, [4, 5], 6, 7, 8]viper; // [1, 2, 3, 4]
forEachRight
从数组右边开始对每个元素执行指定函数
viper arr
例子
viper; // '4', '3', '2', '1'
distinctValuesOfArray
数组去重
viper ...arr
例子
viper; // [1,2,3,4,5]
sampleSize
从右删除指定位置n的元素, n默认为1
viper arr};
例子
viper; // [1,2]viper; // [1]viper; // []
everyNth
返回数组中的每个第n个元素
viper arr;
例子
viper; // [ 2, 4, 6 ]
ArrayConcat
返回数组中唯一值
viper arr
例子
viper; // [1,3,5]
11111111
indexOfAll
返回数组中val出现的所有索引
viper { const indices = arr return indices }
例子
viper; // [0,3]viper; // []
initial
返回除最后一个数组外的所有元素
viper arr
例子
; // [1,2]
initializeArrayWithRange
初始化一个包含指定范围中的数字的数组
viper Array
例子
viper; // [3,4,5,6,7]viper; // [0,2,4,6,8]
initializeArrayWithValues
初始化指定长度并填充初始值
viper Arrayn
例子
viper; // [2,2,2,2,2]
isSorted
是否排序 正序返回1 降序返回-1 未排序返回0
viper { const direction = arr0 > arr1 ? -1 : 1; forlet i val of arr ifi === arrlength - 1 return direction else ifval - arri + 1 * direction > 0 return 0 }
例子
viper; // 1viper; // -1viper; // 0
join
使用分隔符和结束符拼接所有数组中的元素并返回拼接好的字符串
viper arr};
例子
viper; // "pen,pineapple,apple&pen"viper; // "pen,pineapple,apple,pen"viper; // "pen,pineapple,apple,pen"
last
返回最后一个元素
viper arrarrlength - 1;
例子
viper; // 3
longestItem
返回多个可迭代对象中长度最长的一个
viper ...vals0
例子
viper; // 'abcd'viper; // [1, 2, 3, 4, 5]viper; // 'foobar'
maxN
从提供的数组中返回n个最大元素 如果n大于或等于提供的数组长度,则返回原始数组(按降序排列)
viper ...arr
例子
viper; // [3]viper; // [3,2]
minN
从提供的数组中返回n个最小元素。如果n大于或等于提供的数组长度,则返回原始数组(按升序排序)
viper ...arr
例子
viper; // [1]viper; // [1,2]
nthElement
返回数组的第n个元素
viper n > 0 ? arr : arr0
例子
viper; // 'b'viper; // 'a'
partition
根据所提供的函数对每个元素的真实性将这些元素分成两个数组
viper{ return arr }
例子
const users = user: 'barney' age: 36 active: false user: 'fred' age: 40 active: true ;viper; // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]
pull
返回的过滤指定的值后的数组
viper { let argState = Array ? args0 : args; let pulled = arr; arrlength = 0; pulled }
例子
let myArray = 'a' 'b' 'c' 'a' 'b' 'c';viper; // myArray = [ 'b', 'b' ]
pullAtIndex
返回的过滤指定的索引后的数组, 返回过滤的值
viper { let removed = ; let pulled = arr arrlength = 0; pulled return removed }};
例子
let myArray = 'a' 'b' 'c' 'd';let pulled = viper; // myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]
pullAtValue
改变原始数组以过滤出指定的值,返回删除的元素
viper { let removed = pushToRemove = arr mutateTo = arr; arrlength = 0 mutateTo return removed }
例子
let myArray = 'a' 'b' 'c' 'd';let pulled = viper; // myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]
reducedFilter
根据条件过滤一个对象数组,同时过滤未指定的键
viper data
例子
const data = id: 1 name: 'john' age: 24 id: 2 name: 'mike' age: 50 ; viper; // [{ id: 2, name: 'mike'}]
shuffle
洗牌数组
viper { let m = arrlength; while m const i = Math; arrm arri = arri arrm; return arr }
例子
const foo = 1 2 3;viper; // [2,3,1], foo = [1,2,3]
similarity
获取数组交集
viper arr};
例子
viper; // [1,2]
symmetricDifference
返回两个数组之间的不同值
viper { const sA = a sB = b; return ...a ...b }
例子
viper; // [3,4]
tail
返回数组中除第一个元素外的所有元素
viper arrlength > 1 ? arr : arr
例子
viper; // [2,3]viper; // [1]
take
返回从头开始删除n个元素的数组
viper arr
例子
viper; // [1, 2, 3]viper; // []
takeRight
返回从最后删除n个元素的数组
viper arr
例子
viper; // [ 2, 3 ]viper; // [3]
union
返回数组合集
viper Array
例子
viper; // [1,2,3,4]
without
创建一个排除所有给定值的数组
viper arr
例子
viper; // [3]
zip
根据原始数组中的位置进行分组创建新的数组 如果参数数组的长度不一致,那么在未找到值的地方使用undefined
viper { const maxLength = Math; return Array }
例子
viper; // [['a', 1, true], ['b', 2, false]]viper; // [['a', 1, true], [undefined, 2, false]]
zipObject
给定一组有效的属性标识符和一个值数组,返回一个将属性关联到值的对象,未找到值的地方使用undefined
viper props};
例子
viper; // {a: 1, b: 2, c: undefined}viper; // {a: 1, b: 2}
average
求数字数组的平均数
viper arr / arrlength
例子
viper // 2
BROWSER
copyToClipboard
复制字符串到剪贴板
viper { const el = document elvalue = str el elstyleposition = 'absolute' elstyleleft ='-9999px' documentbody const selected = documentrangeCount > 0 ? document : false el document documentbody ifselected document document }
例子
viper // 111复制到了剪贴板
getScrollPosition
返回当前页面的滚动位置
viper x: elpageXOffset !== undefined ? elpageXOffset : elscrollLeft y: elpageYOffset !== undefined ? elpageYOffset : elscrollTop
例子
viper // {x: 3, y: 6023}
getStyle
返回指定元素的CSS规则的值
viper ruleName
例子
viper // 16px
hasClass
元素是否包含类
viper elclassList}
例子
viper // ture
hide
隐藏指定所有元素
viper ...els
例子
viper // 页面中的所以img被隐藏
scrollToTop
平滑返回顶部
viper { const c = documentdocumentElementscrollTop || documentbodyscrollTop ifc > 0 window window }
例子
viper // 页面返回顶部
steStyle
设置元素的css样式
viper{ elstyleruleNmae = val }
例子
viper // 第一个p元素字体大小为18像素
show
显示所有指定的元素
viper ...el
例子
viper // 显示所有img标签
toggleClass
切换元素类名
viper elclassList
例子
viper // 切换box类名
FUNCTION
once
确保函数只调用一次
viper { let called = false; return { ifcalled return called = true return fn } }
例子
documentbody // 函数只会调用一次
debounce
防抖
viper { var timeout result; return { var context = this; var args = arguments; if timeout ; if immediate // 如果已经执行过,不再执行 var callNow = !timeout; timeout = if callNow func else timeout = ; } }
例子
var count = 1;var container = documentbody; { console}; containeronmousemove = viper;
MATH
factorial
阶乘
viper n <= 1 ? 1 : n *
例子
viper // 720
gcd
最大公约数
viper !y ? x : ;
例子
viper // 4viper // 4
inRange
是否在指定范围内
viper { ifend && start > end end = start start = end0 return end == null ? n >= 0 && n < start : n >= start && n < end }
例子
viper // falseviper // true
isDivisible
数值2能否否能整除数值1
viper second % first === 0
例子
viper // true
randomIntegerInRange
随机获取指定范围内的整数
viper Math + min
例子
viper // 3
randomNumberInRange
随机获取指定范围内的小数
viper Math * max - min + min
例子
viper; // 6.0211363285087005
round
返回指定位数的小数,省略第二个参数 四舍五入为整数
viper Number`e-`
例子
viper // 4viper; // 1.01
OBJECT
pick
从对象中挑选与给定键对应的键值对
viper arr
例子
viper; // { 'a': 1, 'c': 3 }
cleanObj
删除指定属性外的其他属性
viper { Object return obj }
例子
const testObj = a: 1 b: 2 children: a: 1 b: 2 ;viper; // { a: 1, children : { a: 1}}
invertKeyValues
反转key和value
viper Object
例子
viper; // { 20: 'age', John: 'name' }
lowercaseKeys
小写所有key
viper Object
例子
const myObj = Name: 'Adam' sUrnAME: 'Smith' ;const myObjLower = viper; // {name: 'Adam', surname: 'Smith'};
merge
合并两个或者多个对象
viper ...objs
例子
const object = a: x: 2 y: 4 b: 1;const other = a: z: 3 b: 2 3 c: 'foo';viper; // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' }
size
获取对象、数组、字符串的大小
viper Array ? vallength : val && typeof val === 'object' ? valsize || vallength || Objectlength : typeof val === 'string' ? valsize : 0
例子
vipersize1 2 3 4 5; // 5vipersize'size'; // 4vipersize one: 1 two: 2 three: 3 ; // 3
STRING
digitize
将数值字符串转换成数组
viper ...``
例子
viper; // [1, 2, 3]
anagrams
生成一个字符串所有的排列组合
viper { ifstrlength <= 2 return strlength === 2 ? str str1 + str0 : str return str }
例子
viper; // ['abc','acb','bac','bca','cab','cba']
byteSize
返回字节大小
viper valsize
例子
viper; // 4viper; // 11
Capitalize
首字母大写
viper first + lowerRest ? rest : rest
例子
viper; // 'FooBar'viper; // 'Foobar'
capitalizeEveryWord
大写单词的每个首字母
viper str
例子
viper; // 'Hello World!'
decapitalize
首字母小写
viper first + upperRest ? rest : rest;
例子
viper; // 'fooBar'viper; // 'fOOBAR'
escapeHTML
转义HTML
viper str
例子
viper; // '<a href="#">Me & you</a>'
fromCamelCase
将驼峰字符改成字符串
viper str
例子
viper; // 'some database field name'viper; // 'some-label-that-needs-to-be-camelized'viper; // 'some_javascript_property'
mask
用指定的字符替换除最后指定个字符以外的所有字符
viper { return '' + str + '' + str }
例子
viper; // '******7890'viper; // '*******890'viper; // '$$$$567890'
palindrome
检查回文
viper { const s = str return s === s }
例子
viper; // true
reverseString
反转字符串
viper ...str
例子
viper // repiv
sortString
按字母顺序排列字符串中的字符
viper ...str
例子
viper; // 'aabbceg'
toCamelCase
将字符串改为驼峰字符串
viper { let s = str && str ; return s + s }
例子
viper; // 'someDatabaseFieldName'viper; // 'someLabelThatNeedsToBeCamelized'viper; // 'someJavascriptProperty'
truncateString
截断字符串在后面添加…
viper { return strlength > num ? str + '...' : str }
例子
; // 'boom...'
unescapeHTML
反转义HTML字符串
viper str
例子
viper; // '<a href="#">Me & you</a>'
TYPE
digitize
校验字符是否是json
viper { try JSON catch e return false }
例子
viper; // trueviper; // falseviper; // true
getType
获取类型 undefined、null、NaN 直接返回 其他返回小写的构造函数的名称
viper v !== v ? 'NaN' : v === undefined ? 'undefined' : v === null ? 'null' : vconstructorname
例子
viper; // 'set'viper // objectviper // array
UTILITY
coalesce
返回第一个非null非undefined的值
viper args
例子
viper; // ""
coalesceFactory
返回第一个符合过滤函数的值
viper args
例子
const customCoalesce = ;viper; // "Waldo"
extendHex
将3位数的hex颜色值转换成6为数的值
viper '#' + shortHex
例子
viper; // '#0033ff'
getURLParams
返回对象包含url上的参数
viper url
例子
viper; // {name: 'Adam', surname: 'Smith'}
hexToRGB
hex转rgb
viper { let alpha = false h = hex; if hlength === 3 h = ...h; else if hlength === 8 alpha = true; h = ; return 'rgb' + alpha ? 'a' : '' + '(' + h >>> alpha ? 24 : 16 + ', ' + h & alpha ? 0x00ff0000 : 0x00ff00 >>> alpha ? 16 : 8 + ', ' + h & alpha ? 0x0000ff00 : 0x0000ff >>> alpha ? 8 : 0 + alpha ? `, ` : '' + ')' ; }
例子
viper; // 'rgba(39, 174, 96, 255)'viper; // 'rgb(39, 174, 96)'viper; // 'rgb(255, 255, 255)'
randomHexCode
随机生成hex颜色值
viper { let n = Math * 0xfffff | 0 return '#' + nlength !== 6 ? Math * 0xf | 0 + n : n }
例子
viper // "#e34155"
RGBToHex
RGB转hex色值
viper r << 16 + g << 8 + b
例子
viper; // 'ffa501'
timeTaken
返回函数运行时长
viper { console const cb = console return cb }
例子
viper; // 1024, (logged): timeTaken: 0.02099609375ms
UUIDGeneratorBrowser
使用crypto API 生成UUID 符合RFC4122 版本 4
viper { return 1e7 + -1e3 + -4e3 + -8e3 + -1e11 }
例子
viper // '7982fcfe-5721-4632-bede-6000885be57d'
validEmail
邮箱验证
viper /^@$/
例子
viper // true