aneka 0.0.25 → 0.1.1
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/index.js +31 -0
- package/package.json +5 -8
- package/src/arrange-array.js +18 -0
- package/src/defaults-deep.js +14 -0
- package/src/delay.js +10 -0
- package/src/extract-text.js +15 -0
- package/src/get-key-by-value.js +5 -0
- package/src/includes.js +11 -0
- package/src/is-class.js +7 -0
- package/src/is-set.js +5 -0
- package/src/log.js +19 -0
- package/src/paginate.js +29 -0
- package/src/pascal-case.js +8 -0
- package/src/round.js +6 -0
- package/src/sec-to-hms.js +20 -0
- package/src/titleize.js +26 -0
- package/src/collection/arrange.js +0 -11
- package/src/collection/break.js +0 -7
- package/src/collection/bulk-remove.js +0 -10
- package/src/collection/bulk-upsert.js +0 -7
- package/src/collection/divide.js +0 -16
- package/src/collection/find-duplicate.js +0 -6
- package/src/collection/paginate.js +0 -13
- package/src/collection/remove.js +0 -6
- package/src/collection/uniq-by-prop.js +0 -11
- package/src/collection/upsert.js +0 -15
- package/src/format/human-join.js +0 -6
- package/src/format/pascal-case.js +0 -5
- package/src/fs/path-resolve.js +0 -7
- package/src/index.js +0 -37
- package/src/loader/get-global-module-dir.js +0 -11
- package/src/loader/get-module-dir-deep.js +0 -25
- package/src/loader/get-module-dir.js +0 -7
- package/src/loader/require-base-deep.js +0 -29
- package/src/loader/require-base.js +0 -19
- package/src/loader/require-deep.js +0 -30
- package/src/loader/require-global.js +0 -5
- package/src/misc/delay.js +0 -18
- package/src/misc/dump.js +0 -6
- package/src/misc/fatal.js +0 -10
- package/src/misc/fetch-url.js +0 -53
- package/src/misc/is-set.js +0 -3
- package/src/misc/parse-bool.js +0 -5
- package/src/misc/print.js +0 -5
- package/src/misc/set-immediate-promise.js +0 -5
- package/src/object/sanitize.js +0 -14
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import arrangeArray from './src/arrange-array.js'
|
|
2
|
+
import defaultsDeep from './src/defaults-deep.js'
|
|
3
|
+
import delay from './src/delay.js'
|
|
4
|
+
import extractText from './src/extract-text.js'
|
|
5
|
+
import getKeyByValue from './get-key-by-value.js'
|
|
6
|
+
import includes from './src/includes.js'
|
|
7
|
+
import isClass from './src/is-class.js'
|
|
8
|
+
import isSet from './src/is-set.js'
|
|
9
|
+
import log from './src/log.js'
|
|
10
|
+
import paginate from './src/paginate.js'
|
|
11
|
+
import pascalCase from './src/pascal-case.js'
|
|
12
|
+
import round from './src/round.js'
|
|
13
|
+
import secToHms from './src/sec-to-hms.js'
|
|
14
|
+
import titleize from './titleize.js'
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
arrangeArray,
|
|
18
|
+
defaultsDeep,
|
|
19
|
+
delay,
|
|
20
|
+
extractText,
|
|
21
|
+
getKeyByValue,
|
|
22
|
+
includes,
|
|
23
|
+
isClass,
|
|
24
|
+
isSet,
|
|
25
|
+
log,
|
|
26
|
+
paginate,
|
|
27
|
+
pascalCase,
|
|
28
|
+
round,
|
|
29
|
+
secToHms,
|
|
30
|
+
titleize
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aneka",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "cemacemla",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "jest"
|
|
8
9
|
},
|
|
@@ -14,18 +15,14 @@
|
|
|
14
15
|
"aneka",
|
|
15
16
|
"functions"
|
|
16
17
|
],
|
|
17
|
-
"author": "Ardhi Lukianto <ardhi@
|
|
18
|
+
"author": "Ardhi Lukianto <ardhi.lukianto@gmail.com>",
|
|
18
19
|
"license": "MIT",
|
|
19
20
|
"bugs": {
|
|
20
21
|
"url": "https://github.com/ardhi/aneka/issues"
|
|
21
22
|
},
|
|
22
23
|
"homepage": "https://github.com/ardhi/aneka#readme",
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"global-dirs": "^3.0.0",
|
|
27
|
-
"lodash": "^4.17.21",
|
|
28
|
-
"query-string": "^7.0.1",
|
|
29
|
-
"worker-timers": "^7.0.63"
|
|
25
|
+
"lodash-es": "^4.17.21",
|
|
26
|
+
"sprintf-js": "^1.1.3"
|
|
30
27
|
}
|
|
31
28
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import trim from 'lodash-es/trim.js'
|
|
2
|
+
|
|
3
|
+
function arrangeArray (inputs = [], trimItem = true) {
|
|
4
|
+
const first = []
|
|
5
|
+
const last = []
|
|
6
|
+
|
|
7
|
+
const items = inputs.filter(item => {
|
|
8
|
+
if (trimItem) item = trim(item)
|
|
9
|
+
if (item[0] === '^') first.push(item.slice(1))
|
|
10
|
+
else if (item[0] === '$') last.push(item.slice(1))
|
|
11
|
+
return !['^', '$'].includes(item[0])
|
|
12
|
+
})
|
|
13
|
+
items.unshift(...first)
|
|
14
|
+
items.push(...last)
|
|
15
|
+
return items
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default arrangeArray
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import mergeWith from 'lodash-es/mergeWith.js'
|
|
2
|
+
import isArray from 'lodash-es/isArray.js'
|
|
3
|
+
|
|
4
|
+
function defaultsDeep (...args) {
|
|
5
|
+
const output = {}
|
|
6
|
+
args.reverse().forEach(function (item) {
|
|
7
|
+
mergeWith(output, item, function (objectValue, sourceValue) {
|
|
8
|
+
return isArray(sourceValue) ? sourceValue : undefined
|
|
9
|
+
})
|
|
10
|
+
})
|
|
11
|
+
return output
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default defaultsDeep
|
package/src/delay.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function extractText (text, patternStart, patternEnd) {
|
|
2
|
+
let result = ''
|
|
3
|
+
const open = text.indexOf(patternStart)
|
|
4
|
+
if (open > -1) {
|
|
5
|
+
text = text.slice(open + patternStart.length)
|
|
6
|
+
const close = text.indexOf(patternEnd)
|
|
7
|
+
if (close > -1) {
|
|
8
|
+
result = text.slice(0, close)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const pattern = `${patternStart}${result}${patternEnd}`
|
|
12
|
+
return { result, pattern }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default extractText
|
package/src/includes.js
ADDED
package/src/is-class.js
ADDED
package/src/is-set.js
ADDED
package/src/log.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { sprintf } from 'sprintf-js'
|
|
2
|
+
|
|
3
|
+
function format (fn, ...items) {
|
|
4
|
+
const date = (new Date()).toISOString()
|
|
5
|
+
const label = typeof items[0] === 'string' ? items[0] : undefined
|
|
6
|
+
if (label) {
|
|
7
|
+
items.shift()
|
|
8
|
+
return fn(sprintf(`[${date}] ${label}`, ...items))
|
|
9
|
+
}
|
|
10
|
+
return fn(...items)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function log (...items) {
|
|
14
|
+
return format(console.log, ...items)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function logError (...items) {
|
|
18
|
+
return format(console.error, ...items)
|
|
19
|
+
}
|
package/src/paginate.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import isPlainObject from 'lodash-es/isPlainObject.js'
|
|
2
|
+
import forOwn from 'lodash-es/forOwn.js'
|
|
3
|
+
import isEmpty from 'lodash-es/isEmpty.js'
|
|
4
|
+
import orderBy from 'lodash-es/orderBy.js'
|
|
5
|
+
|
|
6
|
+
function paginate (collection = [], { page = 1, limit = 25, sort } = {}) {
|
|
7
|
+
const count = collection.length
|
|
8
|
+
const offset = (page - 1) * limit
|
|
9
|
+
const fields = []
|
|
10
|
+
const dirs = []
|
|
11
|
+
if (isPlainObject(sort)) {
|
|
12
|
+
forOwn(sort, (v, k) => {
|
|
13
|
+
fields.push(k)
|
|
14
|
+
dirs.push(v < 0 ? 'desc' : 'asc')
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
if (!isEmpty(fields)) collection = orderBy(collection, fields, dirs)
|
|
18
|
+
const data = collection.slice(offset, offset + limit)
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
data,
|
|
22
|
+
page,
|
|
23
|
+
limit,
|
|
24
|
+
count,
|
|
25
|
+
pages: Math.ceil(collection.length / limit)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default paginate
|
package/src/round.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import padStart from 'lodash-es/padStart.js'
|
|
2
|
+
|
|
3
|
+
export default function secToHms (secs, ms) {
|
|
4
|
+
let remain
|
|
5
|
+
if (ms) {
|
|
6
|
+
remain = secs % 1000
|
|
7
|
+
secs = Math.floor(secs / 1000)
|
|
8
|
+
}
|
|
9
|
+
const secNum = parseInt(secs, 10)
|
|
10
|
+
const hours = Math.floor(secNum / 3600)
|
|
11
|
+
const minutes = Math.floor(secNum / 60) % 60
|
|
12
|
+
const seconds = secNum % 60
|
|
13
|
+
|
|
14
|
+
let hms = [hours, minutes, seconds]
|
|
15
|
+
.map(v => v < 10 ? '0' + v : v)
|
|
16
|
+
.filter((v, i) => v !== '00' || i > 0)
|
|
17
|
+
.join(':')
|
|
18
|
+
if (ms) hms += '+' + padStart(remain, 3, '0')
|
|
19
|
+
return hms
|
|
20
|
+
}
|
package/src/titleize.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import forOwn from 'lodash-es/forOwn.js'
|
|
2
|
+
import map from 'lodash-es/map.js'
|
|
3
|
+
import words from 'lodash-es/words.js'
|
|
4
|
+
import uniq from 'lodash-es/uniq.js'
|
|
5
|
+
import concat from 'lodash-es/concat.js'
|
|
6
|
+
import upperFirst from 'lodash-es/upperFirst.js'
|
|
7
|
+
|
|
8
|
+
function titleize (text = '', { ignores = [], replacement = {} } = {}) {
|
|
9
|
+
const defIgnores = ['or', 'and', 'of', 'with']
|
|
10
|
+
const replacer = {}
|
|
11
|
+
forOwn(replacement, (v, k) => {
|
|
12
|
+
const id = this.generateId('int')
|
|
13
|
+
replacer[id] = k
|
|
14
|
+
text = text.replace(k, ` ${id} `)
|
|
15
|
+
})
|
|
16
|
+
return map(words(text), t => {
|
|
17
|
+
forOwn(replacer, (v, k) => {
|
|
18
|
+
if (k === t) t = replacement[replacer[k]]
|
|
19
|
+
})
|
|
20
|
+
ignores = uniq(concat(ignores, defIgnores))
|
|
21
|
+
if (ignores.includes(t)) return t
|
|
22
|
+
return upperFirst(t)
|
|
23
|
+
}).join(' ')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default titleize
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const { each, concat, reject, find } = require('lodash')
|
|
2
|
-
|
|
3
|
-
module.exports = function (coll = [], col = '', names = []) {
|
|
4
|
-
const picked = []
|
|
5
|
-
const misc = reject(coll, c => names.includes(c[col]))
|
|
6
|
-
each(names, n => {
|
|
7
|
-
const item = find(coll, c => c[col] === n)
|
|
8
|
-
if (item) picked.push(item)
|
|
9
|
-
})
|
|
10
|
-
return concat(picked, misc)
|
|
11
|
-
}
|
package/src/collection/break.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const { findIndex, set, pullAt, each } = require('lodash')
|
|
2
|
-
|
|
3
|
-
module.exports = function (items, collection, key = 'id') {
|
|
4
|
-
const idxs = []
|
|
5
|
-
each(items, item => {
|
|
6
|
-
const idx = findIndex(collection, set({}, key, item[key]))
|
|
7
|
-
if (idx > -1) idxs.push(idx)
|
|
8
|
-
})
|
|
9
|
-
return pullAt(idxs)
|
|
10
|
-
}
|
package/src/collection/divide.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// source: https://flaviocopes.com/how-to-divide-collay-js/
|
|
2
|
-
module.exports = function (coll, groupCount) {
|
|
3
|
-
const result = []
|
|
4
|
-
for (let i = 0; i < groupCount; i++) {
|
|
5
|
-
result.push([])
|
|
6
|
-
}
|
|
7
|
-
const wordsPerLine = Math.ceil(coll.length / groupCount)
|
|
8
|
-
for (let line = 0; line < groupCount; line++) {
|
|
9
|
-
for (let i = 0; i < wordsPerLine; i++) {
|
|
10
|
-
const value = coll[i + line * wordsPerLine]
|
|
11
|
-
if (!value) continue
|
|
12
|
-
result[line].push(value)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return result
|
|
16
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
const { map, filter, includes, uniq } = require('lodash')
|
|
2
|
-
|
|
3
|
-
module.exports = function (collection, prop = 'id') {
|
|
4
|
-
// based on: https://stackoverflow.com/questions/31681732/lodash-get-duplicate-values-from-an-array
|
|
5
|
-
return uniq(filter(map(collection, prop), (val, i, iter) => includes(iter, val, i + 1)))
|
|
6
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
module.exports = function (coll, page = 1, rowsPerPage = 25) {
|
|
2
|
-
const total = coll.length
|
|
3
|
-
const offset = (page - 1) * rowsPerPage
|
|
4
|
-
const rows = coll.slice(offset, offset + rowsPerPage)
|
|
5
|
-
|
|
6
|
-
return {
|
|
7
|
-
rows,
|
|
8
|
-
page,
|
|
9
|
-
rowsPerPage,
|
|
10
|
-
total,
|
|
11
|
-
totalPages: Math.ceil(coll.length / rowsPerPage)
|
|
12
|
-
}
|
|
13
|
-
}
|
package/src/collection/remove.js
DELETED
package/src/collection/upsert.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const { findIndex, set } = require('lodash')
|
|
2
|
-
|
|
3
|
-
module.exports = function (item, collection, key = 'id', tsCol, insert) {
|
|
4
|
-
const idx = findIndex(collection, set({}, key, item[key]))
|
|
5
|
-
if (idx > -1) {
|
|
6
|
-
if (tsCol) {
|
|
7
|
-
if (item[tsCol] > collection[idx][tsCol]) collection[idx] = item
|
|
8
|
-
} else {
|
|
9
|
-
collection[idx] = item
|
|
10
|
-
}
|
|
11
|
-
} else {
|
|
12
|
-
if (insert) collection.unshift(item)
|
|
13
|
-
else collection.push(item)
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/format/human-join.js
DELETED
package/src/fs/path-resolve.js
DELETED
package/src/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
// collection
|
|
3
|
-
arrange: require('./collection/arrange'),
|
|
4
|
-
breakArray: require('./collection/break'),
|
|
5
|
-
bulkRemove: require('./collection/bulk-remove'),
|
|
6
|
-
bulkUpsert: require('./collection/bulk-upsert'),
|
|
7
|
-
divideArray: require('./collection/divide'),
|
|
8
|
-
findDuplicate: require('./collection/find-duplicate'),
|
|
9
|
-
paginate: require('./collection/paginate'),
|
|
10
|
-
removeFromArray: require('./collection/remove'),
|
|
11
|
-
uniqByProp: require('./collection/uniq-by-prop'),
|
|
12
|
-
upsert: require('./collection/upsert'),
|
|
13
|
-
// format
|
|
14
|
-
pascalCase: require('./format/pascal-case'),
|
|
15
|
-
humanJoin: require('./format/human-join'),
|
|
16
|
-
// fs
|
|
17
|
-
pathResolve: require('./fs/path-resolve'),
|
|
18
|
-
// loader
|
|
19
|
-
getGlobalModuleDir: require('./loader/get-global-module-dir'),
|
|
20
|
-
getModuleDir: require('./loader/get-module-dir'),
|
|
21
|
-
getModuleDirDeep: require('./loader/get-module-dir-deep'),
|
|
22
|
-
requireDeep: require('./loader/require-deep'),
|
|
23
|
-
requireBase: require('./loader/require-base'),
|
|
24
|
-
requireBaseDeep: require('./loader/require-base-deep'),
|
|
25
|
-
requireGlobal: require('./loader/require-global'),
|
|
26
|
-
// misc
|
|
27
|
-
delay: require('./misc/delay'),
|
|
28
|
-
fatal: require('./misc/fatal'),
|
|
29
|
-
fetchUrl: require('./misc/fetch-url'),
|
|
30
|
-
isSet: require('./misc/is-set'),
|
|
31
|
-
print: require('./misc/print'),
|
|
32
|
-
parseBool: require('./misc/parse-bool'),
|
|
33
|
-
dump: require('./misc/dump'),
|
|
34
|
-
setImmediatePromise: require('./misc/set-immediate-promise'),
|
|
35
|
-
// object
|
|
36
|
-
sanitizeObject: require('./object/sanitize')
|
|
37
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const globalDirs = require('global-dirs')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const pathResolve = require('../fs/path-resolve')
|
|
4
|
-
|
|
5
|
-
module.exports = function (moduleId) {
|
|
6
|
-
try {
|
|
7
|
-
return pathResolve(path.dirname(require.resolve(path.join(globalDirs.yarn.packages, moduleId, 'package.json'))))
|
|
8
|
-
} catch (err) {
|
|
9
|
-
return pathResolve(path.dirname(require.resolve(path.join(globalDirs.npm.packages, moduleId, 'package.json'))))
|
|
10
|
-
}
|
|
11
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const pathResolve = require('../fs/path-resolve')
|
|
2
|
-
const getGlobalModuleDir = require('./get-global-module-dir')
|
|
3
|
-
const path = require('path')
|
|
4
|
-
|
|
5
|
-
module.exports = function (name) {
|
|
6
|
-
const pkg = name + '/package.json'
|
|
7
|
-
let pkgFile
|
|
8
|
-
try {
|
|
9
|
-
pkgFile = require.resolve(pathResolve(pkg))
|
|
10
|
-
} catch (err) {
|
|
11
|
-
try {
|
|
12
|
-
pkgFile = require.resolve(pkg)
|
|
13
|
-
} catch (err) {
|
|
14
|
-
try {
|
|
15
|
-
pkgFile = require.resolve(process.cwd() + '/node_modules/' + pkg)
|
|
16
|
-
} catch (err) {
|
|
17
|
-
try {
|
|
18
|
-
pkgFile = getGlobalModuleDir(pkg)
|
|
19
|
-
} catch (err) {}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
if (!pkgFile) throw new Error(`Can't get module directory '${name}' as file nor local/global module`)
|
|
24
|
-
return pathResolve(path.dirname(pkgFile))
|
|
25
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const fg = require('fast-glob')
|
|
2
|
-
const { map, filter, isFunction } = require('lodash')
|
|
3
|
-
const path = require('path')
|
|
4
|
-
|
|
5
|
-
const stripExt = file => {
|
|
6
|
-
const parts = path.parse(file)
|
|
7
|
-
return parts.dir + '/' + parts.name
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
module.exports = async function (dir, transformer, options = {}) {
|
|
11
|
-
if (!dir) throw new Error('Directory is missing')
|
|
12
|
-
if (!transformer) throw new Error('Transformer is missing')
|
|
13
|
-
const files = await fg(dir + '/*.{json,js}', options.glob)
|
|
14
|
-
const strippeds = map(files, f => stripExt(f))
|
|
15
|
-
const result = []
|
|
16
|
-
const me = this
|
|
17
|
-
for (const file of files) {
|
|
18
|
-
const stripped = stripExt(file)
|
|
19
|
-
if (filter(strippeds, f => f === stripped).length > 1) throw new (`File '${stripped}.{json,js}' already used`)
|
|
20
|
-
try {
|
|
21
|
-
let mod = require(file)
|
|
22
|
-
if (isFunction(mod)) mod = await mod.call(me, options)
|
|
23
|
-
result.push(await transformer(file, mod, options.transformer))
|
|
24
|
-
} catch (err) {
|
|
25
|
-
throw err
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return result
|
|
29
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const { isFunction } = require('lodash')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const pathResolve = require('../fs/path-resolve')
|
|
4
|
-
|
|
5
|
-
module.exports = async function (file, options) {
|
|
6
|
-
const parsed = path.parse(pathResolve(file))
|
|
7
|
-
const me = this
|
|
8
|
-
try {
|
|
9
|
-
const mod = require(parsed.dir + '/' + parsed.name + '.js')
|
|
10
|
-
if (isFunction(mod)) return await mod.call(me, options)
|
|
11
|
-
return mod
|
|
12
|
-
} catch (err) {
|
|
13
|
-
try {
|
|
14
|
-
return require(parsed.dir + '/' + parsed.name + '.json')
|
|
15
|
-
} catch (err) {
|
|
16
|
-
throw err
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
const getModuleDirDeep = require('./get-module-dir-deep')
|
|
3
|
-
|
|
4
|
-
module.exports = function (name) {
|
|
5
|
-
return require(getModuleDirDeep(name))
|
|
6
|
-
}
|
|
7
|
-
*/
|
|
8
|
-
const requireGlobal = require('./require-global')
|
|
9
|
-
const pathResolve = require('../fs/path-resolve')
|
|
10
|
-
|
|
11
|
-
module.exports = function (name) {
|
|
12
|
-
let mod
|
|
13
|
-
try {
|
|
14
|
-
mod = require(pathResolve(name))
|
|
15
|
-
} catch (err) {
|
|
16
|
-
try {
|
|
17
|
-
mod = require(name)
|
|
18
|
-
} catch (err) {
|
|
19
|
-
try {
|
|
20
|
-
mod = require(process.cwd() + '/node_modules/' + name)
|
|
21
|
-
} catch (err) {
|
|
22
|
-
try {
|
|
23
|
-
mod = requireGlobal(name)
|
|
24
|
-
} catch (err) {}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (!mod) throw new Error(`Can't load '${name}' as file nor local/global module`)
|
|
29
|
-
return mod
|
|
30
|
-
}
|
package/src/misc/delay.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const wt = require('worker-timers')
|
|
2
|
-
|
|
3
|
-
module.exports = function (ms = 1000, useWorker) {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
let timer
|
|
6
|
-
if (useWorker) {
|
|
7
|
-
timer = wt.setTimeout(() => {
|
|
8
|
-
if (timer) wt.clearTimeout(timer)
|
|
9
|
-
resolve()
|
|
10
|
-
}, ms)
|
|
11
|
-
} else {
|
|
12
|
-
timer = setTimeout(() => {
|
|
13
|
-
if (timer) clearTimeout(timer)
|
|
14
|
-
resolve()
|
|
15
|
-
}, ms)
|
|
16
|
-
}
|
|
17
|
-
})
|
|
18
|
-
}
|
package/src/misc/dump.js
DELETED
package/src/misc/fatal.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const print = require('./print')
|
|
2
|
-
const { get } = require('lodash')
|
|
3
|
-
const isSet = require('./is-set')
|
|
4
|
-
|
|
5
|
-
module.exports = function (msg, terminate) {
|
|
6
|
-
if (!isSet(terminate)) terminate = true
|
|
7
|
-
const debug = get(process, 'env.DEBUG', false)
|
|
8
|
-
if (msg instanceof Error) msg = msg.message
|
|
9
|
-
print(msg, terminate, debug ? 'trace' : 'error')
|
|
10
|
-
}
|
package/src/misc/fetch-url.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const axios = require('axios')
|
|
2
|
-
const { has, isString, omit } = require('lodash')
|
|
3
|
-
const qs = require('query-string')
|
|
4
|
-
|
|
5
|
-
const handleSuccess = (resp, opts, cfg) => {
|
|
6
|
-
if (cfg.raw) return resp
|
|
7
|
-
return resp.data
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const handleError = (err, opts, cfg) => {
|
|
11
|
-
return err
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = function (opts = {}, cfg = {}) {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
if (isString(opts)) opts = { url: opts }
|
|
17
|
-
opts.params = opts.params || {}
|
|
18
|
-
if (!has(cfg, 'cacheBuster')) cfg.cacheBuster = true
|
|
19
|
-
if (cfg.cacheBuster) opts.params[cfg.cacheBusterKey || '_'] = Date.now()
|
|
20
|
-
if (cfg.streamHandler) {
|
|
21
|
-
opts.url = `${opts.url}?${qs.stringify(opts.params)}`
|
|
22
|
-
let error
|
|
23
|
-
fetch(opts.url, omit(opts, ['url', 'params']))
|
|
24
|
-
.then(resp => {
|
|
25
|
-
if (!resp.ok) {
|
|
26
|
-
error = resp
|
|
27
|
-
return false
|
|
28
|
-
}
|
|
29
|
-
return cfg.streamHandler(resp)
|
|
30
|
-
})
|
|
31
|
-
.then(resp => {
|
|
32
|
-
if (!resp) return false
|
|
33
|
-
resolve(handleSuccess({ data: resp }, opts, cfg))
|
|
34
|
-
})
|
|
35
|
-
.then(resp => {
|
|
36
|
-
const e = new Error(error.statusText)
|
|
37
|
-
e.status = error.status
|
|
38
|
-
reject(handleError(e, opts, cfg))
|
|
39
|
-
})
|
|
40
|
-
.catch(err => {
|
|
41
|
-
reject(handleError(err, opts, cfg))
|
|
42
|
-
})
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
axios(opts)
|
|
46
|
-
.then(resp => {
|
|
47
|
-
resolve(handleSuccess(resp, opts, cfg))
|
|
48
|
-
})
|
|
49
|
-
.catch(err => {
|
|
50
|
-
reject(handleError(err, opts, cfg))
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
}
|
package/src/misc/is-set.js
DELETED
package/src/misc/parse-bool.js
DELETED
package/src/misc/print.js
DELETED
package/src/object/sanitize.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const { forOwn } = require('lodash')
|
|
2
|
-
|
|
3
|
-
module.exports = function(params) {
|
|
4
|
-
const result = {}
|
|
5
|
-
forOwn(params, (v, k) => {
|
|
6
|
-
let val
|
|
7
|
-
if (v === 'true') val = true
|
|
8
|
-
else if (v === 'false') val = false
|
|
9
|
-
else if (Number(v)) val = Number(v)
|
|
10
|
-
else val = v
|
|
11
|
-
result[k] = val
|
|
12
|
-
})
|
|
13
|
-
return result
|
|
14
|
-
}
|