imagic-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "extends": ["airbnb", "eslint-config-airbnb-base", "prettier"],
3
+ "rules": {
4
+ "indent": ["error", 4],
5
+ "semi": "off",
6
+ "prettier/prettier": ["error"],
7
+ "no-unused-vars": "warn",
8
+ "max-len": ["error", { "code": 150 }],
9
+ "no-console": "off",
10
+ "import/extensions": "off"
11
+ },
12
+ "parserOptions": {
13
+ "ecmaVersion": 2022,
14
+ "sourceType": "module"
15
+ },
16
+ "env": {
17
+ "es6": true,
18
+ "node": true
19
+ },
20
+ "plugins": ["import", "prettier", "node"]
21
+ }
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "singleQuote": true,
3
+ "semi": false,
4
+ "tabWidth": 4,
5
+ "printWidth": 150,
6
+ "arrowParens": "always",
7
+ "bracketSpacing": true,
8
+ "arrayBracketSpacing": true,
9
+ "jsxBracketSameLine": false,
10
+ "trailingComma": "es5"
11
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 iMagicKey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # utils
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "imagic-utils",
3
+ "version": "1.0.0",
4
+ "description": "Utils package",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node ./tests/index.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/iMagicKey/utils.git"
13
+ },
14
+ "keywords": [
15
+ "utils",
16
+ "util"
17
+ ],
18
+ "author": "iMagicKey",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/iMagicKey/utils/issues"
22
+ },
23
+ "homepage": "https://github.com/iMagicKey/utils#readme",
24
+ "engines": {
25
+ "node": ">=18"
26
+ }
27
+ }
package/src/index.js ADDED
@@ -0,0 +1,31 @@
1
+ import getRandArrayItem from './utils/array/getRandArrayItem.js'
2
+ import getRandFloat from './utils/float/getRandFloat.js'
3
+ import chance from './utils/general/chance.js'
4
+ import generateBigId from './utils/general/generateBigId.js'
5
+ import generateId from './utils/general/generateId.js'
6
+ import isJSON from './utils/general/isJSON.js'
7
+ import shuffle from './utils/general/shuffle.js'
8
+ import wait from './utils/general/wait.js'
9
+ import md5 from './utils/general/md5.js'
10
+ import getRandInteger from './utils/integer/getRandInteger.js'
11
+ import getLocalAddress from './utils/os/getLocalAddress.js'
12
+ import getLocalAddresses from './utils/os/getLocalAddresses.js'
13
+ import getCPUUsage from './utils/os/getCPUUsage.js'
14
+ import getRandString from './utils/string/getRandString.js'
15
+
16
+ export default {
17
+ getRandArrayItem,
18
+ getRandFloat,
19
+ chance,
20
+ generateBigId,
21
+ generateId,
22
+ isJSON,
23
+ shuffle,
24
+ wait,
25
+ md5,
26
+ getRandInteger,
27
+ getLocalAddress,
28
+ getLocalAddresses,
29
+ getCPUUsage,
30
+ getRandString,
31
+ }
@@ -0,0 +1,3 @@
1
+ export default function (array) {
2
+ return array[Math.floor(Math.random() * array.length)]
3
+ }
@@ -0,0 +1,8 @@
1
+ export default function (min, max, decimalPoint = 5) {
2
+ let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
3
+ let numString = ''
4
+ for (let i = 0; i < decimalPoint; i++) {
5
+ numString += Math.floor(Math.random() * nums.length)
6
+ }
7
+ return parseFloat(`${this.getRandInteger(min, max)}.${numString})}`)
8
+ }
@@ -0,0 +1,7 @@
1
+ export default function (percent) {
2
+ if (Math.ceil(Math.random() * 100) <= percent) {
3
+ return true
4
+ }
5
+
6
+ return false
7
+ }
@@ -0,0 +1,6 @@
1
+ export default function () {
2
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (e) {
3
+ var t = (16 * Math.random()) | 0
4
+ return ('x' === e ? t : (3 & t) | 8).toString(16)
5
+ })
6
+ }
@@ -0,0 +1,6 @@
1
+ export default function () {
2
+ return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (e) {
3
+ let t = (16 * Math.random()) | 0
4
+ return ('x' === e ? t : (3 & t) | 8).toString(16)
5
+ })
6
+ }
@@ -0,0 +1,7 @@
1
+ export default function (json) {
2
+ try {
3
+ if (JSON.parse(json)) return true
4
+ } catch (e) {
5
+ return false
6
+ }
7
+ }
@@ -0,0 +1,5 @@
1
+ import crypto from 'crypto'
2
+
3
+ export default function (data) {
4
+ return crypto.createHash('md5').update(data).digest('hex')
5
+ }
@@ -0,0 +1,7 @@
1
+ export default function (variable) {
2
+ return Array(variable.length)
3
+ .fill(null)
4
+ .map((_, i) => [Math.random(), i])
5
+ .sort(([a], [b]) => a - b)
6
+ .map(([, i]) => variable[i])
7
+ }
@@ -0,0 +1,7 @@
1
+ export default function (miliseconds) {
2
+ return new Promise((resolve) => {
3
+ setTimeout(() => {
4
+ resolve()
5
+ }, miliseconds)
6
+ })
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function (min, max) {
2
+ return Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min) + 1)) + Math.ceil(min)
3
+ }
@@ -0,0 +1,31 @@
1
+ import os from 'os'
2
+
3
+ export default function (ms = 1000, free = false) {
4
+ function getCPUInfo() {
5
+ let stats = os.cpus().map((cpu) => {
6
+ return {
7
+ total: cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.idle + cpu.times.irq,
8
+ idle: cpu.times.idle,
9
+ }
10
+ })
11
+
12
+ return stats.reduce((a, b) => {
13
+ return {
14
+ total: a.total + b.total,
15
+ idle: a.idle + b.idle,
16
+ }
17
+ })
18
+ }
19
+
20
+ return new Promise((resolve) => {
21
+ const startStats = getCPUInfo()
22
+ setTimeout(() => {
23
+ const endStats = getCPUInfo()
24
+ if (free) {
25
+ resolve((endStats.idle - startStats.idle) / (endStats.total - startStats.total))
26
+ } else {
27
+ resolve(1 - (endStats.idle - startStats.idle) / (endStats.total - startStats.total))
28
+ }
29
+ }, ms)
30
+ })
31
+ }
@@ -0,0 +1,20 @@
1
+ import os from 'os'
2
+
3
+ export default function () {
4
+ let networkInterfaces = os.networkInterfaces()
5
+
6
+ let localAddresses = []
7
+ for (let index in networkInterfaces) {
8
+ let interIPv4 = networkInterfaces[index]
9
+ .filter((inter) => {
10
+ return inter.family == 'IPv4' && inter.address != '127.0.0.1'
11
+ })
12
+ .map((inter) => {
13
+ return inter.address
14
+ })
15
+
16
+ localAddresses = localAddresses.concat(interIPv4)
17
+ }
18
+
19
+ return localAddresses[0]
20
+ }
@@ -0,0 +1,20 @@
1
+ import os from 'os'
2
+
3
+ export default function () {
4
+ let networkInterfaces = os.networkInterfaces()
5
+
6
+ let localAddresses = []
7
+ for (let index in networkInterfaces) {
8
+ let interIPv4 = networkInterfaces[index]
9
+ .filter((inter) => {
10
+ return inter.family == 'IPv4' && inter.address != '127.0.0.1'
11
+ })
12
+ .map((inter) => {
13
+ return inter.address
14
+ })
15
+
16
+ localAddresses = localAddresses.concat(interIPv4)
17
+ }
18
+
19
+ return localAddresses
20
+ }
@@ -0,0 +1,10 @@
1
+ export default function (length = 16, charSet) {
2
+ var result = []
3
+ charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
4
+
5
+ while (length--) {
6
+ result.push(charSet[Math.floor(Math.random() * charSet.length)])
7
+ }
8
+
9
+ return result.join('')
10
+ }
package/tests/index.js ADDED
@@ -0,0 +1,25 @@
1
+ import utils from '../src/index.js'
2
+
3
+ // ARRAY
4
+ console.log(utils.getRandArrayItem([1, 2, 3, 4, 5, 6, 7, 8, 9]))
5
+
6
+ // FLOAT
7
+ console.log(utils.getRandFloat(0, 1, 3))
8
+
9
+ // GENERAL
10
+ console.log(utils.chance(20))
11
+ console.log(utils.generateBigId())
12
+ console.log(utils.generateId())
13
+ console.log(utils.isJSON('{}'))
14
+ console.log(utils.shuffle([1, 2, 3, 4, 5, 6, 7, 8]))
15
+ console.log(utils.wait(1000))
16
+
17
+ // INTEGER
18
+ console.log(utils.getRandInteger(1, 2000))
19
+
20
+ // OS
21
+ console.log(utils.getLocalAddress())
22
+ console.log(utils.getLocalAddresses())
23
+
24
+ // STRING
25
+ console.log(utils.getRandString(2))