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 | - [Str](#str)
|
13 | - [Hash](#hash)
|
14 | - [Random](#random)
|
15 | - [Wrandom](#wrandom)
|
16 | - [Arr](#arr)
|
17 | - [License](#license)
|
18 |
|
19 | ## Install
|
20 |
|
21 | ```sh
|
22 | yarn add suni
|
23 | # or
|
24 | npm install suni
|
25 | ```
|
26 |
|
27 | ## Usage
|
28 |
|
29 | ### Str
|
30 |
|
31 | digit, uppercase, lowercase, alphabet, url safe string.
|
32 |
|
33 | ```js
|
34 | import { Str } from 'suni'
|
35 |
|
36 | console.log(Str.digit) // 0123456789
|
37 |
|
38 | console.log(Str.uppercase) // ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
39 |
|
40 | console.log(Str.lowercase) // abcdefghijklmnopqrstuvwxyz
|
41 |
|
42 | console.log(Str.alphabet) // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
43 |
|
44 | // url safe string
|
45 | console.log(Str.url) // 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz
|
46 | ```
|
47 |
|
48 | ### Hash
|
49 |
|
50 | sum, md5
|
51 |
|
52 | ```js
|
53 | import { Hash } from 'suni'
|
54 |
|
55 | console.log(Hash.sum('')) // bba68bf6
|
56 |
|
57 | console.log(Hash.sum('string')) // ed36c8f2
|
58 |
|
59 | console.log(Hash.sum({ a: {}, b: {} })) // 3718c6e8
|
60 |
|
61 | // Calculate the (hex-encoded) MD5 hash of a given string value:
|
62 | const hash = Hash.md5('value') // "2063c1608d6e0baf80249c42e2be5804"
|
63 |
|
64 | // Calculate the (hex-encoded) HMAC-MD5 hash of a given string value and key:
|
65 | const hash = Hash.md5('value', 'key') // "01433efd5f16327ea4b31144572c67f6"
|
66 |
|
67 | // Calculate the raw MD5 hash of a given string value:
|
68 | const hash = Hash.md5('value', null, true)
|
69 |
|
70 | // Calculate the raw HMAC-MD5 hash of a given string value and key:
|
71 | const hash = Hash.md5('value', 'key', true)
|
72 | ```
|
73 |
|
74 | ### Random
|
75 |
|
76 | Pseudorandom generator: number / string / array item .
|
77 |
|
78 | ```js
|
79 | import { Random } from 'suni'
|
80 |
|
81 | // set `unique` to `true`, random will be consecutively unique.
|
82 | const random = new Random({
|
83 | // seed: 100
|
84 | unique: true
|
85 | })
|
86 |
|
87 | // generate a url safe string
|
88 | console.log(random.string()) // Mo7Lp23PNkW-J_jwzzTH2hEg2XuQSE3
|
89 |
|
90 | console.log(random.int()) // 141279642
|
91 |
|
92 | // 100 ~ 2147483647
|
93 | console.log(random.int(100)) // 1517513188
|
94 |
|
95 | // 1 ~ 100
|
96 | console.log(random.int(1, 100)) // 62
|
97 |
|
98 | console.log(random.float()) // 0.4130089482413688
|
99 |
|
100 | console.log(random.float(100)) // 947894369.301629
|
101 |
|
102 | console.log(random.float(1, 100)) // 57.521107099038645
|
103 |
|
104 | console.log(random.lowercase()) // nebsfcpkqrszwka
|
105 |
|
106 | console.log(random.lowercase(8)) // xgwjbvwf
|
107 |
|
108 | console.log(random.uppercase(8)) // EBEZNDYK
|
109 |
|
110 | console.log(random.alphabet(8)) // IcFqJKIZ
|
111 |
|
112 | console.log(random.digit(8)) // 58441778
|
113 |
|
114 | console.log(random.array(['a', 'b', 'c'])) // c
|
115 | ```
|
116 |
|
117 | ### Wrandom
|
118 |
|
119 | Produce a random integer based on weights.
|
120 |
|
121 | ```js
|
122 | import { Wandom } from 'suni'
|
123 |
|
124 | const weights = [0.2, 0.5, 0.3]
|
125 | console.log(Wandom(weights)) // random index of weights
|
126 |
|
127 | const weightsNotAddTo1 = [0.2, 0.3] // auto reweight to [0.4, 0.6]
|
128 | console.log(Wandom(weightsNotAddTo1)) // random index of weights
|
129 |
|
130 | const items = [
|
131 | {
|
132 | anyValue: 0.1,
|
133 | weight: 0.2
|
134 | },
|
135 | {
|
136 | anyValue: 0.7,
|
137 | weight: 0.5
|
138 | },
|
139 | {
|
140 | anyValue: 0.2,
|
141 | weight: 0.3
|
142 | }
|
143 | ]
|
144 |
|
145 | console.log(Wandom(items)) // random item of items
|
146 | console.log(Wandom(items, item => item.anyValue)) // random item of items, passing a callback function to get weight
|
147 |
|
148 | const obj = {
|
149 | key1: 0.3,
|
150 | key2: 0.5,
|
151 | key3: 0.2
|
152 | }
|
153 |
|
154 | console.log(Wandom(obj)) // random key of obj
|
155 | ```
|
156 |
|
157 | ### Arr
|
158 |
|
159 | shuffle, create
|
160 |
|
161 | ```js
|
162 | import { Arr } from 'suni'
|
163 |
|
164 | const arr = [1, 2, 3, 4, 5]
|
165 |
|
166 | Arr.shuffle(arr)
|
167 |
|
168 | console.log(arr) // [4, 3, 1, 5, 2]
|
169 |
|
170 | console.log(Arr.create(5)) // [0, 1, 2, 3, 4]
|
171 |
|
172 | console.log(Arr.create(5, 10)) // [10, 11, 12, 13, 14]
|
173 |
|
174 | console.log(Arr.create(5, i => i + 10)) // [10, 11, 12, 13, 14]
|
175 | ```
|
176 |
|
177 | ## License
|
178 |
|
179 | [MIT](./LICENSE)
|