@wizhut_tech/wizjs 0.1.5 → 0.1.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.
@@ -0,0 +1,191 @@
1
+ const { isNil, isObject, isFunction } = require('./checks.js');
2
+ const { toInteger } = require('../math/numbers.js');
3
+
4
+
5
+ class Counter {
6
+ constructor(source) {
7
+ this._map = new Map();
8
+
9
+ if (isNil(source)) {
10
+ return;
11
+ }
12
+
13
+ if (source instanceof Counter) {
14
+ for (const [item, n] of source.entries()) {
15
+ this._map.set(item, n);
16
+ }
17
+ return;
18
+ }
19
+
20
+ if (source instanceof Map) {
21
+ for (const [item, n] of source) {
22
+ this.add(item, n);
23
+ }
24
+ return;
25
+ }
26
+
27
+ if (typeof source[Symbol.iterator] === 'function') {
28
+ for (const item of source) {
29
+ this.add(item, 1);
30
+ }
31
+ return;
32
+ }
33
+
34
+ if (isObject(source)) {
35
+ const keys = Object.keys(source);
36
+
37
+ for (let i = 0; i < keys.length; i++) {
38
+ this.add(keys[i], source[keys[i]]);
39
+ }
40
+ }
41
+ }
42
+
43
+ add(item, n) {
44
+ const delta = n === undefined ? 1 : Number(n);
45
+ const next = (this._map.has(item) ? this._map.get(item) : 0) + delta;
46
+ this._map.set(item, next);
47
+ return next;
48
+ }
49
+
50
+ get(item) {
51
+ if (!this._map.has(item)) {
52
+ return 0;
53
+ }
54
+
55
+ return this._map.get(item);
56
+ }
57
+
58
+ set(item, n) {
59
+ this._map.set(item, Number(n));
60
+ return this;
61
+ }
62
+
63
+ delete(item) {
64
+ return this._map.delete(item);
65
+ }
66
+
67
+ mostCommon(n) {
68
+ const entries = [];
69
+
70
+ for (const pair of this._map.entries()) {
71
+ entries.push(pair);
72
+ }
73
+
74
+ entries.sort((a, b) => b[1] - a[1]);
75
+
76
+ if (n === undefined || n === null) {
77
+ return entries;
78
+ }
79
+
80
+ const count = toInteger(n);
81
+
82
+ if (count < 1) {
83
+ return [];
84
+ }
85
+
86
+ return entries.slice(0, count);
87
+ }
88
+
89
+ total() {
90
+ let sum = 0;
91
+
92
+ for (const v of this._map.values()) {
93
+ sum += v;
94
+ }
95
+
96
+ return sum;
97
+ }
98
+
99
+ get size() {
100
+ return this._map.size;
101
+ }
102
+
103
+ entries() {
104
+ return this._map.entries();
105
+ }
106
+
107
+ keys() {
108
+ return this._map.keys();
109
+ }
110
+
111
+ values() {
112
+ return this._map.values();
113
+ }
114
+
115
+ *elements() {
116
+ for (const [item, count] of this._map) {
117
+ const times = count > 0 ? toInteger(count) : 0;
118
+
119
+ for (let i = 0; i < times; i++) {
120
+ yield item;
121
+ }
122
+ }
123
+ }
124
+
125
+ [Symbol.iterator]() {
126
+ return this._map.keys();
127
+ }
128
+ }
129
+
130
+
131
+ class DefaultDict {
132
+ constructor(factory) {
133
+ this._factory = isFunction(factory) ? factory : null;
134
+ this._data = new Map();
135
+ }
136
+
137
+ get(key) {
138
+ if (!this._data.has(key)) {
139
+ if (this._factory === null) {
140
+ return undefined;
141
+ }
142
+
143
+ this._data.set(key, this._factory());
144
+ }
145
+
146
+ return this._data.get(key);
147
+ }
148
+
149
+ peek(key, defaultValue) {
150
+ if (!this._data.has(key)) {
151
+ return defaultValue;
152
+ }
153
+
154
+ return this._data.get(key);
155
+ }
156
+
157
+ set(key, value) {
158
+ this._data.set(key, value);
159
+ return this;
160
+ }
161
+
162
+ has(key) {
163
+ return this._data.has(key);
164
+ }
165
+
166
+ delete(key) {
167
+ return this._data.delete(key);
168
+ }
169
+
170
+ get size() {
171
+ return this._data.size;
172
+ }
173
+
174
+ entries() {
175
+ return this._data.entries();
176
+ }
177
+
178
+ keys() {
179
+ return this._data.keys();
180
+ }
181
+
182
+ values() {
183
+ return this._data.values();
184
+ }
185
+ }
186
+
187
+
188
+ module.exports = {
189
+ Counter,
190
+ DefaultDict
191
+ };
@@ -1,10 +1,143 @@
1
1
 
2
+ const { isNil, isFunction } = require('./checks.js');
3
+
2
4
 
3
5
  function reflexive(arg) {
4
6
  return arg;
5
7
  }
6
8
 
7
9
 
10
+ function partial(fn, ...preset) {
11
+ return function partiallyApplied(...rest) {
12
+ return fn.apply(this, preset.concat(rest));
13
+ };
14
+ }
15
+
16
+
17
+ function compose(...fns) {
18
+ if (fns.length === 0) {
19
+ return reflexive;
20
+ }
21
+
22
+ return function composed(...args) {
23
+ let result = fns[fns.length - 1].apply(this, args);
24
+
25
+ for (let i = fns.length - 2; i >= 0; i--) {
26
+ result = fns[i].call(this, result);
27
+ }
28
+
29
+ return result;
30
+ };
31
+ }
32
+
33
+
34
+ function once(fn) {
35
+ let called = false;
36
+ let result;
37
+
38
+ return function onceWrapped(...args) {
39
+ if (called) {
40
+ return result;
41
+ }
42
+
43
+ called = true;
44
+ result = fn.apply(this, args);
45
+ return result;
46
+ };
47
+ }
48
+
49
+
50
+ const MEMO_RESULT = Symbol('memoize.result');
51
+
52
+
53
+ function memoize(fn, resolver) {
54
+ const cache = new Map();
55
+
56
+ const memoized = function memoized(...args) {
57
+ if (resolver) {
58
+ const key = resolver.apply(this, args);
59
+
60
+ if (cache.has(key)) {
61
+ return cache.get(key);
62
+ }
63
+
64
+ const result = fn.apply(this, args);
65
+ cache.set(key, result);
66
+ return result;
67
+ }
68
+
69
+ let node = cache;
70
+
71
+ for (let i = 0; i < args.length; i++) {
72
+ const arg = args[i];
73
+
74
+ if (!node.has(arg)) {
75
+ node.set(arg, new Map());
76
+ }
77
+
78
+ node = node.get(arg);
79
+ }
80
+
81
+ if (node.has(MEMO_RESULT)) {
82
+ return node.get(MEMO_RESULT);
83
+ }
84
+
85
+ const result = fn.apply(this, args);
86
+ node.set(MEMO_RESULT, result);
87
+ return result;
88
+ };
89
+
90
+ memoized.cache = cache;
91
+ return memoized;
92
+ }
93
+
94
+
95
+ function reduce(fn, iterable, initializer) {
96
+ if (!isFunction(fn) || isNil(iterable) || typeof iterable[Symbol.iterator] !== 'function') {
97
+ if (arguments.length >= 3) {
98
+ return initializer;
99
+ }
100
+
101
+ throw new TypeError('reduce() of empty iterable with no initial value');
102
+ }
103
+
104
+ const iterator = iterable[Symbol.iterator]();
105
+ let acc;
106
+ let started = arguments.length >= 3;
107
+
108
+ if (started) {
109
+ acc = initializer;
110
+ }
111
+
112
+ while (true) {
113
+ const next = iterator.next();
114
+
115
+ if (next.done) {
116
+ break;
117
+ }
118
+
119
+ if (!started) {
120
+ acc = next.value;
121
+ started = true;
122
+ } else {
123
+ acc = fn(acc, next.value);
124
+ }
125
+ }
126
+
127
+ if (!started) {
128
+ throw new TypeError('reduce() of empty iterable with no initial value');
129
+ }
130
+
131
+ return acc;
132
+ }
133
+
134
+
8
135
  module.exports = {
9
- reflexive
10
- }
136
+ reflexive,
137
+ partial,
138
+ compose,
139
+ once,
140
+ memoize,
141
+ cache: memoize,
142
+ reduce
143
+ }