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