@valkyriestudios/utils 11.4.0 → 11.5.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/array/dedupe.js CHANGED
@@ -4,14 +4,14 @@ const fnv1A_1 = require("../hash/fnv1A");
4
4
  function dedupe(val) {
5
5
  if (!Array.isArray(val) || val.length === 0)
6
6
  return [];
7
- const map = new Map();
7
+ const set = new Set();
8
8
  const acc = [];
9
9
  let hash;
10
10
  for (const item of val) {
11
11
  hash = (0, fnv1A_1.default)(item);
12
- if (map.has(hash))
12
+ if (set.has(hash))
13
13
  continue;
14
- map.set(hash, true);
14
+ set.add(hash);
15
15
  acc.push(item);
16
16
  }
17
17
  return acc;
package/array/join.js CHANGED
@@ -4,12 +4,12 @@ const round_1 = require("../number/round");
4
4
  function join(val, opts = {}) {
5
5
  if (!Array.isArray(val) || val.length === 0)
6
6
  return '';
7
- const OPTS = Object.assign({
7
+ const OPTS = {
8
8
  delim: ' ',
9
9
  trim: true,
10
10
  valtrim: true,
11
- valround: false,
12
- }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
11
+ ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
12
+ };
13
13
  const filtered = [];
14
14
  for (const el of val) {
15
15
  if (typeof el === 'string' && el.trim().length > 0) {
package/array/mapFn.js CHANGED
@@ -4,9 +4,10 @@ function mapFn(arr, fn, opts = {}) {
4
4
  if ((!Array.isArray(arr) || arr.length === 0) ||
5
5
  typeof fn !== 'function')
6
6
  return {};
7
- const OPTS = Object.assign({
7
+ const OPTS = {
8
8
  merge: false,
9
- }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
9
+ ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
10
+ };
10
11
  const map = {};
11
12
  let hash = false;
12
13
  for (const el of arr) {
@@ -17,7 +18,7 @@ function mapFn(arr, fn, opts = {}) {
17
18
  continue;
18
19
  hash = `${hash}`;
19
20
  if (OPTS.merge === true && map.hasOwnProperty(hash)) {
20
- map[hash] = Object.assign(map[hash], el);
21
+ map[hash] = { ...map[hash], ...el };
21
22
  }
22
23
  else {
23
24
  map[hash] = el;
package/array/mapKey.js CHANGED
@@ -7,16 +7,17 @@ function mapKey(arr, key, opts = {}) {
7
7
  const key_s = key.trim();
8
8
  if (key_s.length === 0)
9
9
  return {};
10
- const OPTS = Object.assign({
10
+ const OPTS = {
11
11
  merge: false,
12
- }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
12
+ ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
13
+ };
13
14
  const map = {};
14
15
  for (const el of arr) {
15
16
  if (Object.prototype.toString.call(el) !== '[object Object]' ||
16
17
  !Object.prototype.hasOwnProperty.call(el, key_s))
17
18
  continue;
18
19
  if (OPTS.merge === true && map.hasOwnProperty(el[key_s])) {
19
- map[el[key_s]] = Object.assign(map[el[key_s]], el);
20
+ map[el[key_s]] = { ...map[el[key_s]], ...el };
20
21
  }
21
22
  else {
22
23
  map[el[key_s]] = el;
@@ -4,11 +4,12 @@ const round_1 = require("../number/round");
4
4
  function mapPrimitive(arr, opts = {}) {
5
5
  if (!Array.isArray(arr) || arr.length === 0)
6
6
  return {};
7
- const OPTS = Object.assign({
7
+ const OPTS = {
8
8
  valtrim: false,
9
9
  valround: false,
10
10
  keyround: false,
11
- }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
11
+ ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
12
+ };
12
13
  const map = {};
13
14
  for (const el of arr) {
14
15
  if (typeof el === 'string' && el.trim().length > 0) {
package/array/sort.js CHANGED
@@ -69,7 +69,7 @@ function sort(arr, by, dir = 'asc', opts = {}) {
69
69
  nokey_arr.push(el);
70
70
  }
71
71
  else {
72
- prepared_arr.push({ t: by(el), el });
72
+ prepared_arr.push({ t: key, el });
73
73
  }
74
74
  }
75
75
  }
@@ -79,14 +79,23 @@ function sort(arr, by, dir = 'asc', opts = {}) {
79
79
  quickSort(prepared_arr);
80
80
  if (dir === 'desc')
81
81
  prepared_arr.reverse();
82
+ const result = [];
82
83
  if (OPTS.nokey_hide) {
83
- return prepared_arr.map(obj => obj.el);
84
+ for (const obj of prepared_arr)
85
+ result.push(obj.el);
84
86
  }
85
87
  else if (OPTS.nokey_atend) {
86
- return [...prepared_arr.map(obj => obj.el), ...nokey_arr];
88
+ for (const obj of prepared_arr)
89
+ result.push(obj.el);
90
+ for (const el of nokey_arr)
91
+ result.push(el);
87
92
  }
88
93
  else {
89
- return [...nokey_arr, ...prepared_arr.map(obj => obj.el)];
94
+ for (const el of nokey_arr)
95
+ result.push(el);
96
+ for (const obj of prepared_arr)
97
+ result.push(obj.el);
90
98
  }
99
+ return result;
91
100
  }
92
101
  exports.default = sort;
package/hash/fnv1A.js CHANGED
@@ -13,7 +13,7 @@ function fnv1A(data, offset = FNV_32) {
13
13
  sanitized = data;
14
14
  }
15
15
  else if (Number.isFinite(data)) {
16
- sanitized = `${data}`;
16
+ sanitized = String(data);
17
17
  }
18
18
  else if (Array.isArray(data) || Object.prototype.toString.call(data) === '[object Object]') {
19
19
  sanitized = JSON.stringify(data);
@@ -22,7 +22,7 @@ function fnv1A(data, offset = FNV_32) {
22
22
  sanitized = data.toString();
23
23
  }
24
24
  else if (data instanceof Date) {
25
- sanitized = `${data.getTime()}`;
25
+ sanitized = String(data.getTime());
26
26
  }
27
27
  else if (Number.isNaN(data) || data === Infinity) {
28
28
  sanitized = REPL_NAN;
@@ -42,7 +42,8 @@ function fnv1A(data, offset = FNV_32) {
42
42
  else {
43
43
  throw new TypeError('An FNV1A Hash could not be calculated for this datatype');
44
44
  }
45
- for (let i = 0; i < sanitized.length; i++) {
45
+ const len = sanitized.length;
46
+ for (let i = 0; i < len; i++) {
46
47
  hash ^= sanitized.charCodeAt(i);
47
48
  hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
48
49
  }
package/object/merge.js CHANGED
@@ -4,17 +4,25 @@ function merge(target, source = {}) {
4
4
  if (Object.prototype.toString.call(target) !== '[object Object]' ||
5
5
  Object.prototype.toString.call(source) !== '[object Object]')
6
6
  throw new TypeError('Please pass a target and object to merge');
7
- return Object.keys(target).reduce((acc, key) => {
8
- if (Object.prototype.toString.call(target[key]) === '[object Object]' &&
9
- !Array.isArray(target[key])) {
10
- acc[key] = source[key] ? merge(target[key], source[key]) : target[key];
7
+ const acc = {};
8
+ for (const key in target) {
9
+ if (!Object.prototype.hasOwnProperty.call(target, key))
10
+ continue;
11
+ if (typeof target[key] === 'object' &&
12
+ target[key] !== null &&
13
+ !Array.isArray(target[key]) &&
14
+ Object.prototype.hasOwnProperty.call(source, key) &&
15
+ typeof source[key] === 'object' &&
16
+ source[key] !== null &&
17
+ !Array.isArray(source[key])) {
18
+ acc[key] = merge(target[key], source[key]);
11
19
  }
12
20
  else {
13
21
  acc[key] = Object.prototype.hasOwnProperty.call(source, key)
14
22
  ? source[key]
15
23
  : target[key];
16
24
  }
17
- return acc;
18
- }, {});
25
+ }
26
+ return acc;
19
27
  }
20
28
  exports.default = merge;
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "11.4.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "email": "contact@valkyriestudios.be", "url": "www.valkyriestudios.be" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
1
+ { "name": "@valkyriestudios/utils", "version": "11.5.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "email": "contact@valkyriestudios.be", "url": "www.valkyriestudios.be" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }