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
|
24 | yarn add suni
|
25 | # or
|
26 | npm install suni
|
27 | ```
|
28 |
|
29 | ## Usage
|
30 |
|
31 | ### Arr
|
32 |
|
33 | shuffle, create
|
34 |
|
35 | ```js
|
36 | const { Arr } = require('suni')
|
37 |
|
38 | const arr = [1, 2, 3, 4, 5]
|
39 |
|
40 | Arr.shuffle(arr)
|
41 |
|
42 | console.log(arr) // [4, 3, 1, 5, 2]
|
43 |
|
44 | Arr.create(5) // [0, 1, 2, 3, 4]
|
45 |
|
46 | Arr.create(5, 10) // [10, 11, 12, 13, 14]
|
47 |
|
48 | Arr.create(5, i => i + 10) // [10, 11, 12, 13, 14]
|
49 | ```
|
50 |
|
51 | ### Hash
|
52 |
|
53 | sum, md5
|
54 |
|
55 | ```js
|
56 | const { Hash } = require('suni')
|
57 |
|
58 | Hash.sum('') // 'bba68bf6'
|
59 |
|
60 | Hash.sum('string') // 'ed36c8f2'
|
61 |
|
62 | Hash.sum({ a: {}, b: {} }) // '3718c6e8'
|
63 |
|
64 | // Calculate the (hex-encoded) MD5 hash of a given string value:
|
65 | Hash.md5('value') // '2063c1608d6e0baf80249c42e2be5804'
|
66 |
|
67 | // Calculate the (hex-encoded) HMAC-MD5 hash of a given string value and key:
|
68 | Hash.md5('value', 'key') // '01433efd5f16327ea4b31144572c67f6'
|
69 |
|
70 | // Calculate the raw MD5 hash of a given string value:
|
71 | Hash.md5('value', null, true)
|
72 |
|
73 | // Calculate the raw HMAC-MD5 hash of a given string value and key:
|
74 | Hash.md5('value', 'key', true)
|
75 | ```
|
76 |
|
77 | ### Is
|
78 |
|
79 | empty, promise.
|
80 |
|
81 | ```js
|
82 | const { Is } = require('suni')
|
83 |
|
84 | Is.empty([]) // true
|
85 | Is.empty({}) // true
|
86 | Is.empty('') // true
|
87 | Is.empty(null) // true
|
88 | Is.empty(undefined) // true
|
89 | Is.empty(new Map()) // true
|
90 | Is.empty(new Set()) // true
|
91 | Is.empty(new Error()) // true
|
92 |
|
93 | Is.empty(true) // false
|
94 | Is.empty(false) // false
|
95 | Is.empty(['a', 'b']) // false
|
96 | Is.empty({ a: 'b' }) // false
|
97 | Is.empty('string') // false
|
98 | Is.empty(0) // false
|
99 | Is.empty(42) // false
|
100 | Is.empty(function() {}) // false
|
101 | Is.empty(function(a, b) {}) // false
|
102 | Is.empty(new Map([['key', 'value']])) // false
|
103 | Is.empty(new Set([1])) // false
|
104 | Is.empty(new Error('fail')) // false
|
105 |
|
106 | Is.promise({ then:function () {...} }) // true
|
107 | Is.promise(null) // false
|
108 | Is.promise({}) // false
|
109 | Is.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
|
120 | const { Obj } = require('suni')
|
121 |
|
122 | const obj = {
|
123 | foo: true,
|
124 | bar: false
|
125 | }
|
126 |
|
127 | Obj.filter(obj, (key, value) => value === true) // { foo: true }
|
128 |
|
129 | Obj.filter(obj, ['bar']) // { bar: false }
|
130 |
|
131 | Obj.map(obj, (key, value) => [key, !value]) // { foo: false, bar: true }
|
132 |
|
133 | const 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
|
144 | Obj.get(obj2, 'a.b.c') // 1
|
145 |
|
146 | // returns undefined if the full key path does not exist and no default is specified
|
147 | Obj.get(obj2, 'a.b.c.f') // undefined
|
148 |
|
149 | // optional third parameter for default if the full key in path is missing
|
150 | Obj.get(obj2, 'a.b.c.f', 'foo') // 'foo'
|
151 |
|
152 | let foo = { a: 1, b: 2 }
|
153 |
|
154 | // or ~> Obj.set(foo, ['d', 'e', 'f'], 'hello')
|
155 | Obj.set(foo, 'd.e.f', 'hello')
|
156 |
|
157 | console.log(foo) // { a:1, b:2, d:{ e:{ f:'hello' } } }
|
158 |
|
159 | let bar = {}
|
160 |
|
161 | Obj.set(bar, 'a.0.b.0', 1)
|
162 | Obj.set(bar, 'a.0.b.1', 2)
|
163 |
|
164 | console.log(bar) // { a: [{ b: [1, 2] }] }
|
165 | ```
|
166 |
|
167 | ### Random
|
168 |
|
169 | Pseudorandom generator: number / string / array item .
|
170 |
|
171 | ```js
|
172 | const { Random } = require('suni')
|
173 |
|
174 | // set `unique` to `true`, random will be consecutively unique.
|
175 | const random = new Random({
|
176 | // seed: 100
|
177 | unique: true
|
178 | })
|
179 |
|
180 | // generate a url safe string
|
181 | random.string() // 'Mo7Lp23PNkW-J_jwzzTH2hEg2XuQSE3'
|
182 |
|
183 | random.int() // 141279642
|
184 |
|
185 | // 100 ~ 2147483647
|
186 | random.int(100) // 1517513188
|
187 |
|
188 | // 1 ~ 100
|
189 | random.int(1, 100) // 62
|
190 |
|
191 | random.float() // 0.4130089482413688
|
192 |
|
193 | random.float(100) // 947894369.301629
|
194 |
|
195 | random.float(1, 100) // 57.521107099038645
|
196 |
|
197 | random.lowercase() // 'nebsfcpkqrszwka'
|
198 |
|
199 | random.lowercase(8) // 'xgwjbvwf'
|
200 |
|
201 | random.uppercase(8) // 'EBEZNDYK'
|
202 |
|
203 | random.alphabet(8) // 'IcFqJKIZ'
|
204 |
|
205 | random.digit(8) // 58441778
|
206 |
|
207 | random.array(['a', 'b', 'c']) // 'c'
|
208 | ```
|
209 |
|
210 | ### Str
|
211 |
|
212 | digit, uppercase, lowercase, alphabet, url safe string.
|
213 | pad, padLeft, padRight.
|
214 | replaceAll.
|
215 |
|
216 | ```js
|
217 | const { Str } = require('suni')
|
218 |
|
219 | Str.digit // '0123456789'
|
220 |
|
221 | Str.uppercase // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
222 |
|
223 | Str.lowercase // 'abcdefghijklmnopqrstuvwxyz'
|
224 |
|
225 | Str.alphabet // 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
226 |
|
227 | // url safe string
|
228 | Str.url // '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz'
|
229 |
|
230 | // pad / padLeft
|
231 | Str.pad('7', 3, '0') // '007'
|
232 | Str.padLeft('7', 3, '0') // '007'
|
233 |
|
234 | // padRight
|
235 | Str.padRight('1', 4, '0') // '1000'
|
236 |
|
237 | // replaceAll
|
238 | Str.replaceAll('rEplacEAll', 'E', 'e') // 'replaceAll'
|
239 | ```
|
240 |
|
241 | ### Wrandom
|
242 |
|
243 | Produce a random array item based on weights.
|
244 |
|
245 | ```js
|
246 | const { Wrandom } = require('suni')
|
247 |
|
248 | const weights = [0.2, 0.5, 0.3]
|
249 | Wrandom(weights) // random index of weights
|
250 |
|
251 | const weightsNotAddTo1 = [0.2, 0.3] // auto reweight to [0.4, 0.6]
|
252 | Wrandom(weightsNotAddTo1) // random index of weights
|
253 |
|
254 | const 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 |
|
269 | Wrandom(items) // random item of items
|
270 | Wrandom(items, item => item.anyValue) // random item of items, passing a callback function to get weight
|
271 |
|
272 | const obj = {
|
273 | key1: 0.3,
|
274 | key2: 0.5,
|
275 | key3: 0.2
|
276 | }
|
277 |
|
278 | Wrandom(obj) // random key of obj
|
279 | ```
|
280 |
|
281 | ## License
|
282 |
|
283 | [MIT](./LICENSE)
|