aneka 0.0.2 → 0.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aneka",
3
- "version": "0.0.2",
3
+ "version": "0.0.6",
4
4
  "description": "cemacemla",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,8 @@
21
21
  },
22
22
  "homepage": "https://github.com/ardhi/aneka#readme",
23
23
  "dependencies": {
24
- "lodash": "^4.17.21"
24
+ "axios": "^0.24.0",
25
+ "lodash": "^4.17.21",
26
+ "query-string": "^7.0.1"
25
27
  }
26
28
  }
@@ -0,0 +1,11 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ // source: https://flaviocopes.com/how-to-divide-collay-js/
2
+
3
+ module.exports = function (coll, itemCount) {
4
+ return new Array(Math.ceil(coll.length / itemCount))
5
+ .fill()
6
+ .map(_ => coll.splice(0, itemCount))
7
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ const upsert = require('./upsert')
2
+
3
+ module.exports = function (items, collection, key = 'id', tsCol, insert) {
4
+ for (let i = 0; i < items.length; i++) {
5
+ upsert(items[i], collection, key, tsCol, insert)
6
+ }
7
+ }
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1,6 @@
1
+ const { findIndex, set, pullAt } = require('lodash')
2
+
3
+ module.exports = function (item, collection, key = 'id') {
4
+ const idx = findIndex(collection, set({}, key, item[key]))
5
+ return pullAt(collection, [idx])
6
+ }
@@ -1,7 +1,7 @@
1
- module.exports = function (array, prop) {
1
+ module.exports = function (collection, prop) {
2
2
  const result = []
3
3
  const map = new Map()
4
- for (const item of array) {
4
+ for (const item of collection) {
5
5
  if (!map.has(item[prop])) {
6
6
  map.set(item[prop], true)
7
7
  result.push(item)
@@ -0,0 +1,15 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = function (ms = 1000) {
2
+ return new Promise((resolve, reject) => {
3
+ setTimeout(() => {
4
+ resolve()
5
+ }, ms)
6
+ })
7
+ }
@@ -0,0 +1,52 @@
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
+ return resp.data
7
+ }
8
+
9
+ const handleError = (err, opts, cfg) => {
10
+ return err
11
+ }
12
+
13
+ module.exports = function (opts = {}, cfg = {}) {
14
+ return new Promise((resolve, reject) => {
15
+ if (isString(opts)) opts = { url: opts }
16
+ opts.params = opts.params || {}
17
+ if (!has(cfg, 'cacheBuster')) cfg.cacheBuster = true
18
+ if (cfg.cacheBuster) opts.params[cfg.cacheBusterKey || '_'] = Date.now()
19
+ if (cfg.streamHandler) {
20
+ opts.url = `${opts.url}?${qs.stringify(opts.params)}`
21
+ let error
22
+ fetch(opts.url, omit(opts, ['url', 'params']))
23
+ .then(resp => {
24
+ if (!resp.ok) {
25
+ error = resp
26
+ return false
27
+ }
28
+ return cfg.streamHandler(resp)
29
+ })
30
+ .then(resp => {
31
+ if (!resp) return false
32
+ resolve(handleSuccess({ data: resp }, opts, cfg))
33
+ })
34
+ .then(resp => {
35
+ const e = new Error(error.statusText)
36
+ e.status = error.status
37
+ reject(handleError(e, opts, cfg))
38
+ })
39
+ .catch(err => {
40
+ reject(handleError(err, opts, cfg))
41
+ })
42
+ return
43
+ }
44
+ axios(opts)
45
+ .then(resp => {
46
+ resolve(handleSuccess(resp, opts, cfg))
47
+ })
48
+ .catch(err => {
49
+ reject(handleError(err, opts, cfg))
50
+ })
51
+ })
52
+ }
@@ -0,0 +1,3 @@
1
+ module.exports = function (input) {
2
+ return !(input === null || input === undefined)
3
+ }
@@ -0,0 +1,14 @@
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
+ }
@@ -1,16 +0,0 @@
1
- const { findIndex, set } = require('lodash')
2
-
3
- module.exports = function (rec, source, key = 'id', tsCol, insert) {
4
- const idx = findIndex(source, set({}, key, rec[key]))
5
- if (idx > -1) {
6
- if (tsCol) {
7
- if (rec[tsCol] > source[idx][tsCol]) source[idx] = rec
8
- } else {
9
- source[idx] = rec
10
- }
11
- } else {
12
- if (insert) source.unshift(rec)
13
- else source.push(rec)
14
- }
15
- return source
16
- }
@@ -1,8 +0,0 @@
1
- const upsert = require('./upsert')
2
-
3
- module.exports = function (recs, source, key = 'id', tsCol, insert) {
4
- for (let i = 0; i < recs.length; i++) {
5
- source = upsert(recs[i], source, key, tsCol, insert)
6
- }
7
- return source
8
- }