UNPKG

6.95 kBMarkdownView Raw
1# Suni
2
3[![996ICU Licence](<https://img.shields.io/badge/license-NPL%20(The%20996%20Prohibited%20License)-blue.svg>)](https://github.com/996icu/996.ICU/blob/master/LICENSE)
4[![npm](https://badgen.net/npm/v/suni)](https://www.npmjs.com/package/suni)
5[![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)
6[![install size](https://badgen.net/packagephobia/install/suni)](https://packagephobia.now.sh/result?p=suni)
7[![Build Status](https://travis-ci.org/yahtnif/suni.svg?branch=master)](https://travis-ci.org/yahtnif/suni)
8
9## Table of contents
10
11- [Install](#install)
12- [Usage](#usage)
13 - [Arr](#arr)
14 - [Hash](#hash)
15 - [Is](#is)
16 - [Obj](#obj)
17 - [Random](#random)
18 - [Str](#str)
19 - [Wrandom](#wrandom)
20- [License](#license)
21
22## Install
23
24```sh
25yarn add suni
26# or
27npm install suni
28```
29
30## Usage
31
32### Arr
33
34```js
35const { Arr } = require('suni')
36```
37
38#### Arr.create
39
40```js
41// value starting from 1
42Arr.create(5) // [1, 2, 3, 4, 5]
43
44// specify a starting value
45Arr.create(5, 10) // [10, 11, 12, 13, 14]
46Arr.create(5, 0) // [0, 1, 2, 3, 4]
47
48// pass a function
49Arr.create(5, i => i - 1) // [0, 1, 2, 3, 4]
50```
51
52#### Arr.shuffle
53
54```js
55const arr = [1, 2, 3, 4, 5]
56
57Arr.shuffle(arr)
58
59console.log(arr) // [4, 3, 1, 5, 2]
60```
61
62#### Arr.unique
63
64```js
65const arr = [1, 2, 3, 2, 1, 'a', 'b', 'a']
66
67Arr.unique(arr) // [1, 2, 3, 'a', 'b']
68```
69
70#### Arr.count
71
72Count the instances of each value in an array, ignoring any non-string values.
73
74```js
75Arr.count([
76 'foo',
77 'bar',
78 'Bar',
79 451,
80 'bar',
81 'bar',
82 'baz',
83 'foo',
84 null,
85 undefined
86])
87
88// [
89// { value: 'bar', count: 3 },
90// { value: 'foo', count: 2 },
91// { value: 'Bar', count: 1 },
92// { value: 'baz', count: 1 },
93// ]
94
95const packages = ['suni', 'smarkdown', 'suni', 'smarkdown', 'suni', 'randelay']
96Arr.count(packages, {
97 value: 'package',
98 count: 'dependents'
99})
100
101// [
102// { package: 'suni', dependents: 3 },
103// { package: 'smarkdown', dependents: 2 },
104// { package: 'randelay', dependents: 1 }
105// ]
106```
107
108### Hash
109
110```js
111const { Hash } = require('suni')
112```
113
114#### Hash.sum
115
116```js
117Hash.sum('') // 'bba68bf6'
118
119Hash.sum('string') // 'ed36c8f2'
120
121Hash.sum({ a: {}, b: {} }) // '3718c6e8'
122```
123
124#### Hash.md5
125
126```js
127// Calculate the (hex-encoded) MD5 hash of a given string value:
128Hash.md5('value') // '2063c1608d6e0baf80249c42e2be5804'
129
130// Calculate the (hex-encoded) HMAC-MD5 hash of a given string value and key:
131Hash.md5('value', 'key') // '01433efd5f16327ea4b31144572c67f6'
132
133// Calculate the raw MD5 hash of a given string value:
134Hash.md5('value', null, true)
135
136// Calculate the raw HMAC-MD5 hash of a given string value and key:
137Hash.md5('value', 'key', true)
138```
139
140### Is
141
142```js
143const { Is } = require('suni')
144```
145
146#### Is.empty
147
148```js
149Is.empty([]) // true
150Is.empty({}) // true
151Is.empty('') // true
152Is.empty(null) // true
153Is.empty(undefined) // true
154Is.empty(new Map()) // true
155Is.empty(new Set()) // true
156Is.empty(new Error()) // true
157
158Is.empty(true) // false
159Is.empty(false) // false
160Is.empty(['a', 'b']) // false
161Is.empty({ a: 'b' }) // false
162Is.empty('string') // false
163Is.empty(0) // false
164Is.empty(42) // false
165Is.empty(function() {}) // false
166Is.empty(function(a, b) {}) // false
167Is.empty(new Map([['key', 'value']])) // false
168Is.empty(new Set([1])) // false
169Is.empty(new Error('fail')) // false
170```
171
172#### Is.promise
173
174```js
175Is.promise({ then:function () {...} }) // true
176Is.promise(null) // false
177Is.promise({}) // false
178Is.promise({ then: true }) // false
179```
180
181### Obj
182
183```js
184const { Obj } = require('suni')
185```
186
187#### Obj.filter
188
189Filter object keys and values into a new object.
190
191```js
192const obj = {
193 foo: true,
194 bar: false
195}
196
197Obj.filter(obj, (key, value) => value === true) // { foo: true }
198
199Obj.filter(obj, ['bar']) // { bar: false }
200```
201
202#### Obj.map
203
204Map object keys and values into a new object.
205
206```js
207const obj = {
208 foo: true,
209 bar: false
210}
211Obj.map(obj, (key, value) => [key, !value]) // { foo: false, bar: true }
212```
213
214#### Obj.get
215
216Safely get a dot-notated path within a nested object.
217
218```js
219const obj = {
220 a: {
221 b: {
222 c: 1,
223 d: undefined,
224 e: null
225 }
226 }
227}
228
229// use string dot notation for keys
230Obj.get(obj, 'a.b.c') // 1
231
232// returns undefined if the full key path does not exist and no default is specified
233Obj.get(obj, 'a.b.c.f') // undefined
234
235// optional third parameter for default if the full key in path is missing
236Obj.get(obj, 'a.b.c.f', 'foo') // 'foo'
237```
238
239#### Obj.set
240
241Safely writing deep Object values.
242
243```js
244let foo = { a: 1, b: 2 }
245
246// or ~> Obj.set(foo, ['d', 'e', 'f'], 'hello')
247Obj.set(foo, 'd.e.f', 'hello')
248
249console.log(foo) // { a:1, b:2, d:{ e:{ f:'hello' } } }
250
251let bar = {}
252
253Obj.set(bar, 'a.0.b.0', 1)
254Obj.set(bar, 'a.0.b.1', 2)
255
256console.log(bar) // { a: [{ b: [1, 2] }] }
257```
258
259### Random
260
261Pseudorandom generator: number / string / array item .
262
263```js
264const { Random } = require('suni')
265
266// set `unique` to `true`, random will be consecutively unique.
267const random = new Random({
268 // seed: 100
269 unique: true
270})
271
272// generate a url safe string
273random.string() // 'Mo7Lp23PNkW-J_jwzzTH2hEg2XuQSE3'
274
275random.int() // 141279642
276
277// 100 ~ 2147483647
278random.int(100) // 1517513188
279
280// 1 ~ 100
281random.int(1, 100) // 62
282
283random.float() // 0.4130089482413688
284
285random.float(100) // 947894369.301629
286
287random.float(1, 100) // 57.521107099038645
288
289random.lowercase() // 'nebsfcpkqrszwka'
290
291random.lowercase(8) // 'xgwjbvwf'
292
293random.uppercase(8) // 'EBEZNDYK'
294
295random.alphabet(8) // 'IcFqJKIZ'
296
297random.digit(8) // 58441778
298
299random.array(['a', 'b', 'c']) // 'c'
300```
301
302### Str
303
304digit, uppercase, lowercase, alphabet, url safe string.
305pad, padLeft, padRight.
306replaceAll.
307
308```js
309const { Str } = require('suni')
310
311Str.digit // '0123456789'
312
313Str.uppercase // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
314
315Str.lowercase // 'abcdefghijklmnopqrstuvwxyz'
316
317Str.alphabet // 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
318
319// url safe string
320Str.url // '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz'
321
322// pad / padLeft
323Str.pad('7', 3, '0') // '007'
324Str.padLeft('7', 3, '0') // '007'
325
326// padRight
327Str.padRight('1', 4, '0') // '1000'
328
329// replaceAll
330Str.replaceAll('rEplacEAll', 'E', 'e') // 'replaceAll'
331```
332
333### Wrandom
334
335Produce a random array item based on weights.
336
337```js
338const { Wrandom } = require('suni')
339
340const weights = [0.2, 0.5, 0.3]
341Wrandom(weights) // random index of weights
342
343const weightsNotAddTo1 = [0.2, 0.3] // auto reweight to [0.4, 0.6]
344Wrandom(weightsNotAddTo1) // random index of weights
345
346const items = [
347 {
348 anyValue: 0.1,
349 weight: 0.2
350 },
351 {
352 anyValue: 0.7,
353 weight: 0.5
354 },
355 {
356 anyValue: 0.2,
357 weight: 0.3
358 }
359]
360
361Wrandom(items) // random item of items
362Wrandom(items, item => item.anyValue) // random item of items, passing a callback function to get weight
363
364const obj = {
365 key1: 0.3,
366 key2: 0.5,
367 key3: 0.2
368}
369
370Wrandom(obj) // random key of obj
371```
372
373## License
374
375[996ICU](./LICENSE)