UNPKG

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