yet-another-js-utils 0.0.7 → 0.0.9
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/js-utils.js +265 -271
- package/package.json +2 -2
package/js-utils.js
CHANGED
@@ -1,303 +1,297 @@
|
|
1
|
+
module.exports = {
|
2
|
+
// # Nil
|
1
3
|
|
2
|
-
//
|
3
|
-
|
4
|
-
//
|
5
|
-
//
|
6
|
-
//
|
7
|
-
// - 3-letters name n
|
8
|
-
// - single argument
|
4
|
+
// I like the NOT operator
|
5
|
+
// I like strict type checking
|
6
|
+
// Hence a strictly typed equivalent:
|
7
|
+
// - 3-letters name n
|
8
|
+
// - single argument
|
9
9
|
|
10
|
-
/**
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
}
|
10
|
+
/**
|
11
|
+
* Nil
|
12
|
+
* Neither null nor undefined
|
13
|
+
* @param { boolean } exp
|
14
|
+
* @returns { boolean }
|
15
|
+
*/
|
16
|
+
nil(exp) { // values implicitly considered functions
|
17
|
+
if (
|
18
|
+
exp === null
|
19
|
+
|| typeof exp === 'undefined'
|
20
|
+
) {
|
21
|
+
return true;
|
22
|
+
} else {
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
},
|
26
26
|
|
27
|
-
// # Hack
|
27
|
+
// # Hack
|
28
28
|
|
29
|
-
// .slice() uses a closed-open interval
|
30
|
-
// the mathematical convention goes closed-closed
|
29
|
+
// .slice() uses a closed-open interval
|
30
|
+
// the mathematical convention goes closed-closed
|
31
31
|
|
32
|
-
/**
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
}
|
32
|
+
/**
|
33
|
+
* Hack
|
34
|
+
* @param { number } start
|
35
|
+
* @param { number } end
|
36
|
+
* @returns
|
37
|
+
*/
|
38
|
+
hack(array, start, end) {
|
39
|
+
return array.slice(start, end + 1);
|
40
|
+
},
|
41
41
|
|
42
|
-
// # Prune/Pick
|
42
|
+
// # Prune/Pick
|
43
43
|
|
44
|
-
// Javascript inherited spreadsheet-like .filter()
|
45
|
-
// It felt confusing there
|
46
|
-
// It feels confusing here too
|
44
|
+
// Javascript inherited spreadsheet-like .filter()
|
45
|
+
// It felt confusing there
|
46
|
+
// It feels confusing here too
|
47
47
|
|
48
|
-
/**
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
}
|
48
|
+
/**
|
49
|
+
* Pick
|
50
|
+
* @param {*} array
|
51
|
+
* @param {*} pruningFunction
|
52
|
+
* @returns
|
53
|
+
*/
|
54
|
+
pick(array, pruningFunction) {
|
55
|
+
return array.filter(pruningFunction);
|
56
|
+
},
|
57
57
|
|
58
|
-
/**
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
}
|
58
|
+
/**
|
59
|
+
* Prune
|
60
|
+
* The inverse of filter()
|
61
|
+
* @param {*} array
|
62
|
+
* @param {*} filteringFunction
|
63
|
+
* @returns
|
64
|
+
*/
|
65
|
+
prune(array, filteringFunction) {
|
66
|
+
return array.filter(function (x) {
|
67
|
+
return !filteringFunction(x);
|
68
|
+
});
|
69
|
+
},
|
70
70
|
|
71
|
-
// # Check
|
71
|
+
// # Check
|
72
72
|
|
73
|
-
// .every() just sounds weird
|
74
|
-
/**
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
}
|
73
|
+
// .every() just sounds weird
|
74
|
+
/**
|
75
|
+
* Check
|
76
|
+
* @param {*} array
|
77
|
+
* @param {*} checkingFunction
|
78
|
+
* @returns
|
79
|
+
*/
|
80
|
+
check(array, checkingFunction) {
|
81
|
+
return array.every(checkingFunction);
|
82
|
+
},
|
83
83
|
|
84
|
-
/**
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
}
|
84
|
+
/**
|
85
|
+
* Escape
|
86
|
+
* @param { string } string
|
87
|
+
* @returns
|
88
|
+
*/
|
89
|
+
escape(string) {
|
90
|
+
return string
|
91
|
+
.replace(/&/g, '&')
|
92
|
+
.replace(/</g, '<')
|
93
|
+
.replace(/>/g, '>')
|
94
|
+
.replace(/"/g, '"')
|
95
|
+
.replace(/'/g, ''');
|
96
|
+
},
|
97
97
|
|
98
|
-
/**
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
return fragment;
|
113
|
-
}
|
98
|
+
/**
|
99
|
+
* DOM-rendering template tag
|
100
|
+
* @param {Array} strings
|
101
|
+
* @returns {object} a DocumentFragment
|
102
|
+
*/
|
103
|
+
html(strings) {
|
104
|
+
let output = strings[0], // assumes empty string start?
|
105
|
+
max = Math.max(strings.length, arguments.length - 1);
|
106
|
+
for (let i = 1; i < max; i++) {
|
107
|
+
output += arguments[i];
|
108
|
+
output += strings[i];
|
109
|
+
}
|
110
|
+
return output;
|
111
|
+
},
|
114
112
|
|
115
|
-
/**
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
113
|
+
/**
|
114
|
+
* Basic contents updater
|
115
|
+
* Assuming:
|
116
|
+
* - secondNode already holds states updates
|
117
|
+
* - rendering applied event-listeners already
|
118
|
+
* @param {*} firstNode
|
119
|
+
* @param {*} secondNode
|
120
|
+
*/
|
121
|
+
replaceChildren(firstNode, secondNode) {
|
122
|
+
let firstNodeChildren = firstNode.childNodes,
|
123
|
+
secondNodeChildren = secondNode.childNodes;
|
124
|
+
for (let i = 0, c = firstNodeChildren.length; i < c; i++) {
|
125
|
+
if (
|
126
|
+
secondNodeChildren[i]
|
127
|
+
&& firstNodeChildren[i].outerHTML !== secondNodeChildren[i].outerHTML
|
128
|
+
) {
|
129
|
+
firstNodeChildren[i].parentNode.replaceChild(firstNodeChildren[i], secondNodeChildren[i]);
|
130
|
+
}
|
132
131
|
}
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
132
|
+
if (firstNodeChildren.length < secondNodeChildren.length) {
|
133
|
+
for (let i = firstNodeChildren.length, c = secondNodeChildren.length; i < c; i++) {
|
134
|
+
firstNodeChildren[i].parentNode.appendChild(secondNodeChildren[i])
|
135
|
+
}
|
137
136
|
}
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
137
|
+
if (firstNodeChildren.length > secondNodeChildren.length) {
|
138
|
+
for (let i = firstNodeChildren.length - 1; i > secondNodeChildren.length - 1; i--) {
|
139
|
+
firstNodeChildren[i].remove();
|
140
|
+
}
|
142
141
|
}
|
143
|
-
}
|
144
|
-
}
|
142
|
+
},
|
145
143
|
|
146
|
-
// # DATA-STATE
|
144
|
+
// # DATA-STATE
|
147
145
|
|
148
|
-
// Alternative CSS state management
|
149
|
-
// - using data-attributes instead of classes
|
150
|
-
// - easier to match model and vue
|
151
|
-
// - dataset over classlist allows specialization
|
152
|
-
// - classes work as booleans (".walked")
|
153
|
-
// - you need to notice abscence
|
154
|
-
// - data-state work as a string type you can dedicate to store state only, and more than one state
|
146
|
+
// Alternative CSS state management
|
147
|
+
// - using data-attributes instead of classes
|
148
|
+
// - easier to match model and vue
|
149
|
+
// - dataset over classlist allows specialization
|
150
|
+
// - classes work as booleans (".walked")
|
151
|
+
// - you need to notice abscence
|
152
|
+
// - data-state work as a string type you can dedicate to store state only, and more than one state
|
155
153
|
|
156
|
-
/**
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
}
|
154
|
+
/**
|
155
|
+
* Add
|
156
|
+
* @param { object } element
|
157
|
+
* @param { string } state
|
158
|
+
*/
|
159
|
+
addDataState(element, state) {
|
160
|
+
let stateArray = [];
|
161
|
+
if (
|
162
|
+
element.dataset.state
|
163
|
+
&& element.dataset.state.indexOf(' ') > -1
|
164
|
+
) {
|
165
|
+
stateArray = element.dataset.state.split(' ');
|
166
|
+
} else {
|
167
|
+
stateArray = [element.dataset.state];
|
168
|
+
}
|
169
|
+
if (stateArray.indexOf(state) === -1) {
|
170
|
+
stateArray.push(state);
|
171
|
+
element.dataset.state = stateArray.join(' ');
|
172
|
+
}
|
173
|
+
},
|
176
174
|
|
177
|
-
/**
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
}
|
175
|
+
/**
|
176
|
+
* Remove
|
177
|
+
* @param { object } element
|
178
|
+
* @param { string } state
|
179
|
+
*/
|
180
|
+
removeDataState(element, state) {
|
181
|
+
let stateArray = [];
|
182
|
+
if (
|
183
|
+
element.dataset.state
|
184
|
+
&& element.dataset.state.indexOf(' ') > -1
|
185
|
+
) {
|
186
|
+
stateArray = element.dataset.state.split(' ');
|
187
|
+
} else {
|
188
|
+
stateArray = [element.dataset.state];
|
189
|
+
}
|
190
|
+
element.dataset.state =
|
191
|
+
stateArray
|
192
|
+
.filter(element => (element !== state))
|
193
|
+
.join(' ');
|
194
|
+
},
|
197
195
|
|
198
|
-
/**
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
}
|
196
|
+
/**
|
197
|
+
* Replace
|
198
|
+
* @param { object } element
|
199
|
+
* @param { string } stateToRemove
|
200
|
+
* @param { string } stateToAdd
|
201
|
+
*/
|
202
|
+
replaceDataState(element, stateToRemove, stateToAdd) {
|
203
|
+
this.removeDataState(element, stateToRemove);
|
204
|
+
this.addDataState(element, stateToAdd);
|
205
|
+
},
|
208
206
|
|
209
|
-
/**
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
}
|
207
|
+
/**
|
208
|
+
* Toggle
|
209
|
+
* @param { object } element
|
210
|
+
* @param { string } state
|
211
|
+
*/
|
212
|
+
toggleDataState(element, state) {
|
213
|
+
let stateArray = element.dataset.state.split(' ');
|
214
|
+
if (stateArray.indexOf(state) == -1) {
|
215
|
+
this.addDataState(element, state);
|
216
|
+
} else {
|
217
|
+
this.removeDataState(element, state);
|
218
|
+
}
|
219
|
+
},
|
222
220
|
|
223
|
-
//# AJAX Functions
|
221
|
+
//# AJAX Functions
|
224
222
|
|
225
|
-
/**
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
223
|
+
/**
|
224
|
+
* AJAX GET
|
225
|
+
* @param {string} url The target url
|
226
|
+
* @param {function} callback
|
227
|
+
* @param {boolean} isJson Response contains json
|
228
|
+
* @param {object} callbackContext
|
229
|
+
*/
|
230
|
+
ajaxGet(url, callback, isJson, callbackContext) {
|
231
|
+
var req = new XMLHttpRequest();
|
232
|
+
req.open("GET", url);
|
233
|
+
req.addEventListener("load", function () {
|
234
|
+
if (req.status >= 200 && req.status < 400) {
|
235
|
+
if (isJson) {
|
236
|
+
var json = {};
|
237
|
+
try {
|
238
|
+
json = JSON.parse(req.responseText);
|
239
|
+
} catch (error) {
|
240
|
+
console.error("Get request returned invalid JSON.")
|
241
|
+
}
|
242
|
+
callbackContext === undefined ? callback(json) : callback.apply(callbackContext, [json]);
|
243
|
+
} else {
|
244
|
+
callbackContext === undefined ? callback(req.responseText) : callback.apply(callbackContext, [req.responseText]);
|
243
245
|
}
|
244
|
-
callbackContext === undefined ? callback(json) : callback.apply(callbackContext, [json]);
|
245
246
|
} else {
|
246
|
-
|
247
|
+
console.error(req.status + " " + req.statusText + " " + url);
|
247
248
|
}
|
248
|
-
}
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
});
|
255
|
-
req.send(null);
|
256
|
-
}
|
249
|
+
});
|
250
|
+
req.addEventListener("error", function () {
|
251
|
+
console.error("Network error trying to access: " + url);
|
252
|
+
});
|
253
|
+
req.send(null);
|
254
|
+
},
|
257
255
|
|
258
|
-
/**
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
256
|
+
/**
|
257
|
+
* AJAX POST
|
258
|
+
* @param {string} url
|
259
|
+
* @param {string} data
|
260
|
+
* @param {function} successCallback
|
261
|
+
* @param {function} failureCallback
|
262
|
+
* @param {boolean} isJson
|
263
|
+
* @param {object} successCallbackContext
|
264
|
+
* @param {object} failureCallbackContext
|
265
|
+
*/
|
266
|
+
ajaxPost(url, data, successCallback, failureCallback, isJson, successCallbackContext, failureCallbackContext) {
|
267
|
+
var req = new XMLHttpRequest();
|
268
|
+
req.open("POST", url);
|
269
|
+
req.addEventListener("load", function () {
|
270
|
+
if (req.status >= 200 && req.status < 400) {
|
271
|
+
successCallbackContext === undefined ? successCallback(req) : successCallback.apply(successCallbackContext, [req]);
|
272
|
+
} else {
|
273
|
+
failureCallbackContext === undefined ? failureCallback(req) : failureCallback.apply(failureCallbackContext, [req]);
|
274
|
+
console.error(req.status + " " + req.statusText + " " + url);
|
275
|
+
}
|
276
|
+
});
|
277
|
+
req.addEventListener("error", function () {
|
278
|
+
console.error("Network error trying to access: " + url);
|
279
|
+
});
|
280
|
+
if (isJson) {
|
281
|
+
req.setRequestHeader("Content-Type", "application/json");
|
282
|
+
data = JSON.stringify(data);
|
277
283
|
}
|
278
|
-
|
279
|
-
|
280
|
-
console.error("Network error trying to access: " + url);
|
281
|
-
});
|
282
|
-
if (isJson) {
|
283
|
-
req.setRequestHeader("Content-Type", "application/json");
|
284
|
-
data = JSON.stringify(data);
|
285
|
-
}
|
286
|
-
req.send(data);
|
287
|
-
}
|
284
|
+
req.send(data);
|
285
|
+
},
|
288
286
|
|
289
|
-
/**
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
287
|
+
/**
|
288
|
+
* Pipe
|
289
|
+
* Function piping with initial input
|
290
|
+
* @param {*} functions
|
291
|
+
* @param {*} input
|
292
|
+
* @returns {*}
|
293
|
+
*/
|
294
|
+
pipe(input, ...functions) {
|
295
|
+
return functions.reduce((res, fun) => fun(res), input);
|
296
|
+
}
|
298
297
|
}
|
299
|
-
|
300
|
-
module.exports = {
|
301
|
-
nil, hack, pick, prune, check, escape, html, replaceChildren, addDataState, removeDataState, replaceDataState,
|
302
|
-
toggleDataState, ajaxGet, ajaxPost, pipe
|
303
|
-
};
|
package/package.json
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
"name": "yet-another-js-utils",
|
3
3
|
"description": "Basic javascript utils",
|
4
4
|
"authors": "Adrian Turcev",
|
5
|
-
"version": "0.0.
|
5
|
+
"version": "0.0.9",
|
6
6
|
"license": "MPL-2.0",
|
7
7
|
"homepage": "https://github.com/adrianturcev/js-utils",
|
8
8
|
"repository": {
|
@@ -13,7 +13,7 @@
|
|
13
13
|
"url": "https://github.com/adrianturcev/js-utils/issues"
|
14
14
|
},
|
15
15
|
"keywords": ["javascript", "utils"],
|
16
|
-
"main": ".js",
|
16
|
+
"main": "js-utils.js",
|
17
17
|
"scripts": {
|
18
18
|
"watch": "npx grunt watch",
|
19
19
|
"test": "npx grunt test",
|