UNPKG

5.71 kBMarkdownView Raw
1# Suni
2
3[![npm](https://badgen.net/npm/v/suni)](https://www.npmjs.com/package/suni)
4[![gzip size](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/suni/dist/suni.js?compression=gzip)](https://cdn.jsdelivr.net/npm/suni/dist/suni.js)
5[![install size](https://badgen.net/packagephobia/install/suni)](https://packagephobia.now.sh/result?p=suni)
6[![Build Status](https://travis-ci.org/yahtnif/suni.svg?branch=master)](https://travis-ci.org/yahtnif/suni)
7
8## Table of contents
9
10- [Install](#install)
11- [Usage](#usage)
12 - [Arr](#arr)
13 - [Hash](#hash)
14 - [Is](#is)
15 - [Obj](#obj)
16 - [Random](#random)
17 - [Str](#str)
18 - [Wrandom](#wrandom)
19- [License](#license)
20
21## Install
22
23```sh
24yarn add suni
25# or
26npm install suni
27```
28
29## Usage
30
31### Arr
32
33shuffle, create
34
35```js
36const { Arr } = require('suni')
37
38const arr = [1, 2, 3, 4, 5]
39
40Arr.shuffle(arr)
41
42console.log(arr) // [4, 3, 1, 5, 2]
43
44Arr.create(5) // [0, 1, 2, 3, 4]
45
46Arr.create(5, 10) // [10, 11, 12, 13, 14]
47
48Arr.create(5, i => i + 10) // [10, 11, 12, 13, 14]
49```
50
51### Hash
52
53sum, md5
54
55```js
56const { Hash } = require('suni')
57
58Hash.sum('') // 'bba68bf6'
59
60Hash.sum('string') // 'ed36c8f2'
61
62Hash.sum({ a: {}, b: {} }) // '3718c6e8'
63
64// Calculate the (hex-encoded) MD5 hash of a given string value:
65Hash.md5('value') // '2063c1608d6e0baf80249c42e2be5804'
66
67// Calculate the (hex-encoded) HMAC-MD5 hash of a given string value and key:
68Hash.md5('value', 'key') // '01433efd5f16327ea4b31144572c67f6'
69
70// Calculate the raw MD5 hash of a given string value:
71Hash.md5('value', null, true)
72
73// Calculate the raw HMAC-MD5 hash of a given string value and key:
74Hash.md5('value', 'key', true)
75```
76
77### Is
78
79empty, promise.
80
81```js
82const { Is } = require('suni')
83
84Is.empty([]) // true
85Is.empty({}) // true
86Is.empty('') // true
87Is.empty(null) // true
88Is.empty(undefined) // true
89Is.empty(new Map()) // true
90Is.empty(new Set()) // true
91Is.empty(new Error()) // true
92
93Is.empty(true) // false
94Is.empty(false) // false
95Is.empty(['a', 'b']) // false
96Is.empty({ a: 'b' }) // false
97Is.empty('string') // false
98Is.empty(0) // false
99Is.empty(42) // false
100Is.empty(function() {}) // false
101Is.empty(function(a, b) {}) // false
102Is.empty(new Map([['key', 'value']])) // false
103Is.empty(new Set([1])) // false
104Is.empty(new Error('fail')) // false
105
106Is.promise({ then:function () {...} }) // true
107Is.promise(null) // false
108Is.promise({}) // false
109Is.promise({ then: true }) // false
110```
111
112### Obj
113
114- **filter**: filter object keys and values into a new object.
115- **map**: map object keys and values into a new object.
116- **get**: safely get a dot-notated path within a nested object.
117- **set**: safely writing deep Object values.
118
119```js
120const { Obj } = require('suni')
121
122const obj = {
123 foo: true,
124 bar: false
125}
126
127Obj.filter(obj, (key, value) => value === true) // { foo: true }
128
129Obj.filter(obj, ['bar']) // { bar: false }
130
131Obj.map(obj, (key, value) => [key, !value]) // { foo: false, bar: true }
132
133const obj2 = {
134 a: {
135 b: {
136 c: 1,
137 d: undefined,
138 e: null
139 }
140 }
141}
142
143// use string dot notation for keys
144Obj.get(obj2, 'a.b.c') // 1
145
146// returns undefined if the full key path does not exist and no default is specified
147Obj.get(obj2, 'a.b.c.f') // undefined
148
149// optional third parameter for default if the full key in path is missing
150Obj.get(obj2, 'a.b.c.f', 'foo') // 'foo'
151
152let foo = { a: 1, b: 2 }
153
154// or ~> Obj.set(foo, ['d', 'e', 'f'], 'hello')
155Obj.set(foo, 'd.e.f', 'hello')
156
157console.log(foo) // { a:1, b:2, d:{ e:{ f:'hello' } } }
158
159let bar = {}
160
161Obj.set(bar, 'a.0.b.0', 1)
162Obj.set(bar, 'a.0.b.1', 2)
163
164console.log(bar) // { a: [{ b: [1, 2] }] }
165```
166
167### Random
168
169Pseudorandom generator: number / string / array item .
170
171```js
172const { Random } = require('suni')
173
174// set `unique` to `true`, random will be consecutively unique.
175const random = new Random({
176 // seed: 100
177 unique: true
178})
179
180// generate a url safe string
181random.string() // 'Mo7Lp23PNkW-J_jwzzTH2hEg2XuQSE3'
182
183random.int() // 141279642
184
185// 100 ~ 2147483647
186random.int(100) // 1517513188
187
188// 1 ~ 100
189random.int(1, 100) // 62
190
191random.float() // 0.4130089482413688
192
193random.float(100) // 947894369.301629
194
195random.float(1, 100) // 57.521107099038645
196
197random.lowercase() // 'nebsfcpkqrszwka'
198
199random.lowercase(8) // 'xgwjbvwf'
200
201random.uppercase(8) // 'EBEZNDYK'
202
203random.alphabet(8) // 'IcFqJKIZ'
204
205random.digit(8) // 58441778
206
207random.array(['a', 'b', 'c']) // 'c'
208```
209
210### Str
211
212digit, uppercase, lowercase, alphabet, url safe string.
213pad, padLeft, padRight.
214replaceAll.
215
216```js
217const { Str } = require('suni')
218
219Str.digit // '0123456789'
220
221Str.uppercase // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
222
223Str.lowercase // 'abcdefghijklmnopqrstuvwxyz'
224
225Str.alphabet // 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
226
227// url safe string
228Str.url // '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz'
229
230// pad / padLeft
231Str.pad('7', 3, '0') // '007'
232Str.padLeft('7', 3, '0') // '007'
233
234// padRight
235Str.padRight('1', 4, '0') // '1000'
236
237// replaceAll
238Str.replaceAll('rEplacEAll', 'E', 'e') // 'replaceAll'
239```
240
241### Wrandom
242
243Produce a random array item based on weights.
244
245```js
246const { Wrandom } = require('suni')
247
248const weights = [0.2, 0.5, 0.3]
249Wrandom(weights) // random index of weights
250
251const weightsNotAddTo1 = [0.2, 0.3] // auto reweight to [0.4, 0.6]
252Wrandom(weightsNotAddTo1) // random index of weights
253
254const items = [
255 {
256 anyValue: 0.1,
257 weight: 0.2
258 },
259 {
260 anyValue: 0.7,
261 weight: 0.5
262 },
263 {
264 anyValue: 0.2,
265 weight: 0.3
266 }
267]
268
269Wrandom(items) // random item of items
270Wrandom(items, item => item.anyValue) // random item of items, passing a callback function to get weight
271
272const obj = {
273 key1: 0.3,
274 key2: 0.5,
275 key3: 0.2
276}
277
278Wrandom(obj) // random key of obj
279```
280
281## License
282
283[MIT](./LICENSE)