utilitish 0.0.6 → 0.0.7

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.
@@ -1,25 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("../utils");
4
- (0, utils_1.defineIfNotExists)(Array.prototype, 'first', function () {
3
+ const core_utils_1 = require("../utils/core.utils");
4
+ const logic_utils_1 = require("../utils/logic.utils");
5
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'first', function () {
5
6
  return this[0];
6
7
  });
7
- (0, utils_1.defineIfNotExists)(Array.prototype, 'last', function () {
8
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'last', function () {
8
9
  return this[this.length - 1];
9
10
  });
10
- (0, utils_1.defineIfNotExists)(Array.prototype, 'sum', function (selector) {
11
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'sum', function (selector) {
11
12
  if (this.length === 0)
12
13
  return 0;
13
- const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
14
+ const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
14
15
  if (this.every((item) => typeof getValue(item) === 'number')) {
15
16
  return this.reduce((acc, item) => getValue(item) + acc, 0);
16
17
  }
17
- throw new TypeError('Array.prototype.sum() requires a callback who return a number unless array is number[]');
18
+ throw new TypeError('Array.prototype.sum() requires a selector who return a number unless array is number[]');
18
19
  });
19
- (0, utils_1.defineIfNotExists)(Array.prototype, 'unique', function () {
20
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'unique', function () {
20
21
  return [...new Set(this)];
21
22
  });
22
- (0, utils_1.defineIfNotExists)(Array.prototype, 'chunk', function (size) {
23
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'chunk', function (size) {
23
24
  if (typeof size !== 'number' || !Number.isInteger(size) || size <= 0) {
24
25
  throw new TypeError('Chunk size must be a positive integer');
25
26
  }
@@ -29,17 +30,17 @@ const utils_1 = require("../utils");
29
30
  }
30
31
  return result;
31
32
  });
32
- (0, utils_1.defineIfNotExists)(Array.prototype, 'average', function (selector) {
33
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'average', function (selector) {
33
34
  if (this.length === 0)
34
35
  return 0;
35
- const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
36
+ const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
36
37
  if (this.every((item) => typeof getValue(item) === 'number')) {
37
38
  return this.reduce((acc, item) => getValue(item) + acc, 0) / this.length;
38
39
  }
39
- throw new TypeError('Array.prototype.average() requires a callback who return a number unless array is number[]');
40
+ throw new TypeError('Array.prototype.average() requires a selector who return a number unless array is number[]');
40
41
  });
41
- (0, utils_1.defineIfNotExists)(Array.prototype, 'groupBy', function (selector) {
42
- const getKey = (0, utils_1.resolveSelector)(selector, (item) => item);
42
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'groupBy', function (selector) {
43
+ const getKey = (0, core_utils_1.resolveSelector)(selector, (item) => item);
43
44
  const map = new Map();
44
45
  for (const item of this) {
45
46
  const key = getKey(item);
@@ -50,24 +51,24 @@ const utils_1 = require("../utils");
50
51
  }
51
52
  return map;
52
53
  });
53
- (0, utils_1.defineIfNotExists)(Array.prototype, 'compact', function () {
54
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'compact', function () {
54
55
  return this.filter(Boolean);
55
56
  });
56
- (0, utils_1.defineIfNotExists)(Array.prototype, 'enumerate', function () {
57
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'enumerate', function () {
57
58
  return this.map((value, index) => [value, index]);
58
59
  });
59
- (0, utils_1.defineIfNotExists)(Array.prototype, 'sortBy', function (direction, selector) {
60
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortBy', function (direction, selector) {
60
61
  if (this.length === 0)
61
62
  return this.slice();
62
- const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
63
- if (!selector && !this.every((item) => (0, utils_1.isNumberOrString)(item))) {
63
+ const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
64
+ if (!selector && !this.every((item) => (0, core_utils_1.isNumberOrString)(item))) {
64
65
  throw new TypeError('Array elements must be number or string if no selector is provided');
65
66
  }
66
67
  return this.slice().sort((a, b) => {
67
68
  const valA = getValue(a);
68
69
  const valB = getValue(b);
69
- if (!(0, utils_1.isNumberOrString)(valA) || !(0, utils_1.isNumberOrString)(valB)) {
70
- throw new TypeError('Callback must return number or string');
70
+ if (!(0, core_utils_1.isNumberOrString)(valA) || !(0, core_utils_1.isNumberOrString)(valB)) {
71
+ throw new TypeError('Selector must return number or string');
71
72
  }
72
73
  if (valA > valB)
73
74
  return direction === 'asc' ? 1 : -1;
@@ -76,13 +77,13 @@ const utils_1 = require("../utils");
76
77
  return 0;
77
78
  });
78
79
  });
79
- (0, utils_1.defineIfNotExists)(Array.prototype, 'sortAsc', function (selector) {
80
- return sortBy(this, 'asc', selector);
80
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortAsc', function (selector) {
81
+ return (0, logic_utils_1.sortBy)(this, 'asc', selector);
81
82
  });
82
- (0, utils_1.defineIfNotExists)(Array.prototype, 'sortDesc', function (selector) {
83
- return sortBy(this, 'desc', selector);
83
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortDesc', function (selector) {
84
+ return (0, logic_utils_1.sortBy)(this, 'desc', selector);
84
85
  });
85
- (0, utils_1.defineIfNotExists)(Array.prototype, 'swap', function (i, j) {
86
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'swap', function (i, j) {
86
87
  if (typeof i !== 'number' || typeof j !== 'number' || !Number.isInteger(i) || !Number.isInteger(j)) {
87
88
  throw new TypeError('Indices must be integers');
88
89
  }
@@ -96,7 +97,7 @@ const utils_1 = require("../utils");
96
97
  }
97
98
  return this;
98
99
  });
99
- (0, utils_1.defineIfNotExists)(Array.prototype, 'shuffle', function () {
100
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'shuffle', function () {
100
101
  const arr = this.slice();
101
102
  for (let i = arr.length - 1; i > 0; i--) {
102
103
  const j = Math.floor(Math.random() * (i + 1));
@@ -104,13 +105,13 @@ const utils_1 = require("../utils");
104
105
  }
105
106
  return arr;
106
107
  });
107
- (0, utils_1.defineIfNotExists)(Array.prototype, 'toMap', function (keySelector, valueSelector) {
108
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'toMap', function (keySelector, valueSelector) {
108
109
  if (!keySelector && this.length && this.every((item) => Array.isArray(item) && item.length === 2)) {
109
110
  return new Map(this);
110
111
  }
111
112
  const map = new Map();
112
- const getKey = (0, utils_1.resolveSelector)(keySelector, (_, index) => index);
113
- const getValue = (0, utils_1.resolveSelector)(valueSelector, (item) => item);
113
+ const getKey = (0, core_utils_1.resolveSelector)(keySelector, (_, index) => index);
114
+ const getValue = (0, core_utils_1.resolveSelector)(valueSelector, (item) => item);
114
115
  for (let i = 0; i < this.length; i++) {
115
116
  const item = this[i];
116
117
  const key = getKey(item, i);
@@ -119,12 +120,16 @@ const utils_1 = require("../utils");
119
120
  }
120
121
  return map;
121
122
  });
122
- (0, utils_1.defineIfNotExists)(Array.prototype, 'toSet', function (selector) {
123
- const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
123
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'toObject', function (keySelector, valueSelector) {
124
+ const map = this.toMap(keySelector, valueSelector);
125
+ return (0, logic_utils_1.mapToObject)(map);
126
+ });
127
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'toSet', function (selector) {
128
+ const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
124
129
  return new Set(this.map(getValue));
125
130
  });
126
- (0, utils_1.defineIfNotExists)(Array.prototype, 'countBy', function (selector) {
127
- const getKey = (0, utils_1.resolveSelector)(selector, (item) => item);
131
+ (0, core_utils_1.defineIfNotExists)(Array.prototype, 'countBy', function (selector) {
132
+ const getKey = (0, core_utils_1.resolveSelector)(selector, (item) => item);
128
133
  const map = new Map();
129
134
  for (const item of this) {
130
135
  const key = getKey(item);
@@ -132,23 +137,3 @@ const utils_1 = require("../utils");
132
137
  }
133
138
  return map;
134
139
  });
135
- function sortBy(arr, direction, selector) {
136
- if (arr.length === 0)
137
- return arr.slice();
138
- const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
139
- if (!selector && !arr.every((item) => (0, utils_1.isNumberOrString)(item))) {
140
- throw new TypeError('Array elements must be number or string if no selector is provided');
141
- }
142
- return arr.slice().sort((a, b) => {
143
- const valA = getValue(a);
144
- const valB = getValue(b);
145
- if (!(0, utils_1.isNumberOrString)(valA) || !(0, utils_1.isNumberOrString)(valB)) {
146
- throw new TypeError('Callback must return number or string');
147
- }
148
- if (valA > valB)
149
- return direction === 'asc' ? 1 : -1;
150
- if (valA < valB)
151
- return direction === 'asc' ? -1 : 1;
152
- return 0;
153
- });
154
- }
@@ -0,0 +1 @@
1
+ import './array-prototype';