script.io.js 1.0.1248 → 1.22.1015

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.
@@ -69,8 +69,61 @@ Define (Object.type_of, "string", function (input) { if (arguments.length) retur
69
69
  * xxx://xxx.xxx.xxx/xxx
70
70
  */
71
71
 
72
- Define (Array.prototype, "json", function () { return JSON.stringify (this); });
72
+ Define (Array.prototype, "json", function () { return JSON.pretty (this); });
73
+ Define (Array.prototype, "clone", function () { return JSON.parse (JSON.stringify (this)); });
73
74
  Define (Array.prototype, "exist", function (input, offset) { return this.includes (input, offset); });
75
+ Define (Array.prototype, "first", function () { for (var i in this) return this [i]; return undefined; }); Define (Array.prototype, "one", function () { return this.first (); });
76
+ Define (Array.prototype, "last", function () { var value; for (var i in this) value = this [i]; return value; });
77
+ Define (Array.prototype, "shuffle", function () { var array = this.clone (); var current = array.length, random; while (current !== 0) { random = Math.floor (Math.random () * current); current --; [array [current], array [random]] = [array [random], array [current]]; } return array; });
78
+ Define (Array.prototype, "limit", function (limit) { return this.clone ().slice (0, limit); });
79
+ Define (Array.prototype, "page", function (page, limit) { return this.clone ().slice (((page - 1) * (limit || Array.config.page.limit)), (page * (limit || Array.config.page.limit))); });
80
+ Define (Array.prototype, "distinct", function (key = "id", distinct = []) { return this.filter (function (array) { if (distinct.includes (array [key])) return false; else { distinct.push (array [key]); return true; } }); });
81
+
82
+ Define (Array.prototype, "select", function (filter) {
83
+ return this.filter (function (array, index) {
84
+ var error = 0;
85
+ for (var i in filter) {
86
+ if (typeof filter [i] === "object") {
87
+ if (filter [i].key === "!") {
88
+ if (filter [i].value !== array [i]) continue;
89
+ else error ++;
90
+ }
91
+ }
92
+ else if (Array.isArray (filter [i])) if (filter [i].includes (array [i])) continue; else error ++;
93
+ else if (Array.isArray (array [i])) if (array [i].includes (filter [i])) continue; else error ++;
94
+ else if (filter [i] === array [i]) continue;
95
+ else error ++;
96
+ }
97
+ if (error) return false;
98
+ else return true;
99
+ });
100
+ });
101
+
102
+ Define (Array.prototype, "order", function (sort) {
103
+ var array = this.clone (), type = "string";
104
+ var object = sort.split (".");
105
+ if (object.length > 2) {
106
+ if (this.length) if (typeof this [0][object [0]][object [1]][object [2]] === "number") type = "number";
107
+ if (type === "string") array.sort (function (a, b) { return a [object [0]][object [1]][object [2]].localeCompare (b [object [0]][object [1]][object [2]]); });
108
+ if (type === "number") array.sort (function (a, b) { return a [object [0]][object [1]][object [2]] - b [object [0]][object [1]][object [2]]; });
109
+ }
110
+ else if (object.length > 1) {
111
+ if (this.length) if (typeof this [0][object [0]][object [1]] === "number") type = "number";
112
+ if (type === "string") array.sort (function (a, b) { return a [object [0]][object [1]].localeCompare (b [object [0]][object [1]]); });
113
+ if (type === "number") array.sort (function (a, b) { return a [object [0]][object [1]] - b [object [0]][object [1]]; });
114
+ }
115
+ else {
116
+ if (this.length) if (typeof this [0][sort] === "number") type = "number";
117
+ if (type === "string") array.sort (function (a, b) { return a [sort].localeCompare (b [sort]); });
118
+ if (type === "number") array.sort (function (a, b) { return a [sort] - b [sort]; });
119
+ }
120
+ return array;
121
+ });
122
+
123
+ Define (Array, "config", function (array = {}) {
124
+ if ("page" in array) if ("limit" in array.page) Array.config.page.limit = array.page.limit;
125
+ });
126
+ Define (Array.config, "page", {limit: 20}, {writable: true});
74
127
 
75
128
  /**
76
129
  * string
@@ -121,6 +174,64 @@ Define (Date, "time", function () { return Date.now (); });
121
174
  Define (Date, "timeout", function (context, second = 1) { return setTimeout (context, (second * 1000)); });
122
175
  Define (Date.timeout, "clear", function (context) { return clearTimeout (context); });
123
176
 
177
+ /**
178
+ * event
179
+ *
180
+ * title
181
+ * description
182
+ * sub description
183
+ *
184
+ * xxx://xxx.xxx.xxx/xxx
185
+ */
186
+
187
+ Define (Event, "io", class {
188
+ constructor () {
189
+ this.data = {}
190
+ }
191
+ on (key, value) {
192
+ if (this.data [key]) this.data [key].push (value);
193
+ else this.data [key] = [value];
194
+ }
195
+ emit (key, ... value) {
196
+ var e;
197
+ for (var i in this.data [key]) {
198
+ e = this.data [key][i] (... value);
199
+ }
200
+ return e;
201
+ }
202
+ });
203
+
204
+ Define (Event, "proto", function (proto) {
205
+ proto.event = proto.event || Object.create (null);
206
+ proto.on = function (key, value) {
207
+ if (proto.event [key]) proto.event [key].push (value);
208
+ else proto.event [key] = [value];
209
+ }
210
+ proto.emit = function (key, ... value) {
211
+ var e;
212
+ for (var i in proto.event [key]) {
213
+ e = proto.event [key][i] (... value);
214
+ }
215
+ return e;
216
+ }
217
+ });
218
+
219
+ /**
220
+ * promise
221
+ *
222
+ * title
223
+ * description
224
+ * sub description
225
+ *
226
+ * xxx://xxx.xxx.xxx/xxx
227
+ */
228
+
229
+ Define (Promise, "io", function (context) {
230
+ return new Promise (function (resolve, reject) {
231
+ context (function (value = true) { resolve (value); }, function (error = "") { reject (error); });
232
+ });
233
+ });
234
+
124
235
  /**
125
236
  * url
126
237
  *
@@ -191,6 +302,34 @@ Define (URL.header, "status", {
191
302
 
192
303
  Define (JSON, "pretty", function (json) { return JSON.stringify (json, null, "\t"); });
193
304
 
305
+ Define (JSON, "db", class {
306
+ constructor (config) {
307
+ this.dir = config.dir;
308
+ this.collection = config.collection || {}
309
+ }
310
+ select (collection) { return new JSON.db.select (this, collection); }
311
+ });
312
+
313
+ Define (JSON.db, "select", class {
314
+ constructor (db, collection) {
315
+ this.db = db;
316
+ this.collection = {name: collection, file: JSON.db.file (collection)}
317
+ if (this.db.collection [this.collection.name]) this.collection.data = this.db.collection [this.collection.name];
318
+ else this.collection.data = JSON.parse (Function.file.read (Function.path.join (this.db.dir, `${this.collection.file}.json`)) || "[]");
319
+ }
320
+ find (filter = {}) {
321
+ var __ = function (db) {
322
+ return new Promise (function (resolve, reject) {
323
+ resolve ({collection: db.collection.name, data: db.collection.data.select (filter)});
324
+ });
325
+ }
326
+ return __ (this);
327
+ }
328
+ });
329
+
330
+ Define (JSON.db, "file", function (key, value) { if (value) return JSON.db.file.object [key] = value; else return JSON.db.file.object [key] || key; });
331
+ Define (JSON.db.file, "object", {});
332
+
194
333
  /**
195
334
  * miscellaneous
196
335
  *
@@ -212,7 +351,6 @@ Define (JSON, "pretty", function (json) { return JSON.stringify (json, null, "\t
212
351
  */
213
352
 
214
353
  Define (Function, "path", function () {});
215
- Define (Function.path, "r", function (path) { return "./" + path; });
216
354
  Define (Function.path, "require", function () { return Function.path.api = require ("path"); });
217
355
  Define (Function.path, "join", function (... path) { return Function.path.api.join (... path); });
218
356
 
@@ -231,6 +369,36 @@ Define (Function.file, "require", function () { return Function.file.api = requi
231
369
  Define (Function.file, "write", function (file, content, context) { if (context) return Function.file.api.writeFile (file, content, context); else return Function.file.api.writeFileSync (file, content); });
232
370
  Define (Function.file, "read", function (file, context) { if (context) return Function.file.api.readFile (file, context); else return Function.file.api.readFileSync (file); });
233
371
 
372
+ /**
373
+ * xxx
374
+ *
375
+ * title
376
+ * description
377
+ * sub description
378
+ *
379
+ * xxx://xxx.xxx.xxx/xxx
380
+ */
381
+
382
+ /**
383
+ * xxx
384
+ *
385
+ * title
386
+ * description
387
+ * sub description
388
+ *
389
+ * xxx://xxx.xxx.xxx/xxx
390
+ */
391
+
392
+ /**
393
+ * xxx
394
+ *
395
+ * title
396
+ * description
397
+ * sub description
398
+ *
399
+ * xxx://xxx.xxx.xxx/xxx
400
+ */
401
+
234
402
  /**
235
403
  * html
236
404
  *
@@ -275,7 +443,7 @@ Define (Function, "html", class {
275
443
  this.head.meta ({"http-equiv": "X-UA-Compatible", content: "IE=edge"});
276
444
  this.head.meta ({"http-equiv": "X-Cross-Origin", content: "*"});
277
445
  this.head.meta ({name: "viewport", content: this.var ["viewport"] || "width=device-width, initial-scale=1.0, maximum-scale=3.0, user-scalable=1"});
278
- this.head.meta ({name: "generator", content: this.var ["generator"] || "Express (5.2.1) Vue (3.5.27)"});
446
+ this.head.meta ({name: "generator", content: this.var ["generator"] || "Netizen" || "Express (5.2.1) Vue (3.5.27)"});
279
447
  this.head.meta ({name: "author", content: this.var ["author"] || "Netizen"});
280
448
  this.head.meta ({name: "description", content: this.var.description});
281
449
  this.head.meta ({name: "keywords", content: this.var ["keyword"]});
@@ -398,40 +566,11 @@ Define (Function.html, "markup", class {
398
566
  * xxx://xxx.xxx.xxx/xxx
399
567
  */
400
568
 
401
- /**
402
- * xxx
403
- *
404
- * title
405
- * description
406
- * sub description
407
- *
408
- * xxx://xxx.xxx.xxx/xxx
409
- */
410
-
411
- /**
412
- * xxx
413
- *
414
- * title
415
- * description
416
- * sub description
417
- *
418
- * xxx://xxx.xxx.xxx/xxx
419
- */
420
-
421
- /**
422
- * xxx
423
- *
424
- * title
425
- * description
426
- * sub description
427
- *
428
- * xxx://xxx.xxx.xxx/xxx
429
- */
430
-
431
569
  Symbol.export = {
432
570
  define: Object.define,
433
571
  object: Object, array: Array, string: String, number: Number, function: Function,
434
- date: Date, time: Date.time, url: URL, json: JSON,
572
+ date: Date, time: Date.time, event: Event, promise: Promise,
573
+ url: URL, json: JSON,
435
574
  path: Function.path, file: Function.file, dir: Function.dir,
436
575
  html: Function.html,
437
576
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "script.io.js",
3
3
  "description": "Hello World",
4
- "version": "1.0.1248",
4
+ "version": "1.022.1015",
5
5
  "author": "Seindi Rahmat Barus <xseindi@gmail.com>",
6
6
  "contributors": [
7
7
  "Seindi Rahmat Barus <xseindi@gmail.com>"