
Table of contents
Install
yarn add suni
npm install suni
Usage
Arr
const { Arr } = require('suni')
Arr.create
Arr.create(5)
Arr.create(5, 10)
Arr.create(5, 0)
Arr.create(5, i => i - 1)
Arr.shuffle
const arr = [1, 2, 3, 4, 5]
Arr.shuffle(arr)
console.log(arr)
Arr.unique
const arr = [1, 2, 3, 2, 1, 'a', 'b', 'a']
Arr.unique(arr)
Arr.chunks
Split array into chunks.
const arr = [1, 2, 3, 4, 5]
Arr.chunks(arr, 2)
Arr.count
Count the instances of each value in an array, ignoring any non-string values.
Arr.count([
'foo',
'bar',
'Bar',
451,
'bar',
'bar',
'baz',
'foo',
null,
undefined
])
const packages = ['suni', 'smarkdown', 'suni', 'smarkdown', 'suni', 'randelay']
Arr.count(packages, {
value: 'package',
count: 'dependents'
})
Hash
const { Hash } = require('suni')
Hash.sum
Hash.sum('')
Hash.sum('string')
Hash.sum({ a: {}, b: {} })
Hash.md5
Hash.md5('value')
Hash.md5('value', 'key')
Hash.md5('value', null, true)
Hash.md5('value', 'key', true)
Is
const { Is } = require('suni')
Is.empty
Is.empty([])
Is.empty({})
Is.empty('')
Is.empty(null)
Is.empty(undefined)
Is.empty(new Map())
Is.empty(new Set())
Is.empty(new Error())
Is.empty(true)
Is.empty(false)
Is.empty(['a', 'b'])
Is.empty({ a: 'b' })
Is.empty('string')
Is.empty(0)
Is.empty(42)
Is.empty(function() {})
Is.empty(function(a, b) {})
Is.empty(new Map([['key', 'value']]))
Is.empty(new Set([1]))
Is.empty(new Error('fail'))
Is.promise
Is.promise({ then:function () {...} })
Is.promise(null)
Is.promise({})
Is.promise({ then: true })
Obj
const { Obj } = require('suni')
Obj.filter
Filter object keys and values into a new object.
const obj = {
foo: true,
bar: false
}
Obj.filter(obj, (key, value) => value === true)
Obj.filter(obj, ['bar'])
Obj.map
Map object keys and values into a new object.
const obj = {
foo: true,
bar: false
}
Obj.map(obj, (key, value) => [key, !value])
Obj.get
Safely get a dot-notated path within a nested object.
const obj = {
a: {
b: {
c: 1,
d: undefined,
e: null
}
}
}
Obj.get(obj, 'a.b.c')
Obj.get(obj, 'a.b.c.f')
Obj.get(obj, 'a.b.c.f', 'foo')
Obj.set
Safely writing deep Object values.
let foo = { a: 1, b: 2 }
Obj.set(foo, 'd.e.f', 'hello')
console.log(foo)
let bar = {}
Obj.set(bar, 'a.0.b.0', 1)
Obj.set(bar, 'a.0.b.1', 2)
console.log(bar)
Random
Pseudorandom generator: number / string / array item .
const { Random } = require('suni')
const random = new Random({
unique: true
})
random.string()
random.int()
random.int(100)
random.int(1, 100)
random.float()
random.float(100)
random.float(1, 100)
random.lowercase()
random.lowercase(8)
random.uppercase(8)
random.alphabet(8)
random.digit(8)
random.array(['a', 'b', 'c'])
Str
digit, uppercase, lowercase, alphabet, url safe string.
pad, padLeft, padRight.
replaceAll.
const { Str } = require('suni')
Str.digit
Str.uppercase
Str.lowercase
Str.alphabet
Str.url
Str.pad('7', 3, '0')
Str.padLeft('7', 3, '0')
Str.padRight('1', 4, '0')
Str.replaceAll('rEplacEAll', 'E', 'e')
Wrandom
Produce a random array item based on weights.
const { Wrandom } = require('suni')
const weights = [0.2, 0.5, 0.3]
Wrandom(weights)
const weightsNotAddTo1 = [0.2, 0.3]
Wrandom(weightsNotAddTo1)
const items = [
{
anyValue: 0.1,
weight: 0.2
},
{
anyValue: 0.7,
weight: 0.5
},
{
anyValue: 0.2,
weight: 0.3
}
]
Wrandom(items)
Wrandom(items, item => item.anyValue)
const obj = {
key1: 0.3,
key2: 0.5,
key3: 0.2
}
Wrandom(obj)
License
Anti 996