script.io.js 1.0.1611 → 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
@@ -249,6 +302,34 @@ Define (URL.header, "status", {
249
302
 
250
303
  Define (JSON, "pretty", function (json) { return JSON.stringify (json, null, "\t"); });
251
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
+
252
333
  /**
253
334
  * miscellaneous
254
335
  *
@@ -270,7 +351,6 @@ Define (JSON, "pretty", function (json) { return JSON.stringify (json, null, "\t
270
351
  */
271
352
 
272
353
  Define (Function, "path", function () {});
273
- Define (Function.path, "r", function (path) { return "./" + path; });
274
354
  Define (Function.path, "require", function () { return Function.path.api = require ("path"); });
275
355
  Define (Function.path, "join", function (... path) { return Function.path.api.join (... path); });
276
356
 
@@ -289,6 +369,36 @@ Define (Function.file, "require", function () { return Function.file.api = requi
289
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); });
290
370
  Define (Function.file, "read", function (file, context) { if (context) return Function.file.api.readFile (file, context); else return Function.file.api.readFileSync (file); });
291
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
+
292
402
  /**
293
403
  * html
294
404
  *
@@ -333,7 +443,7 @@ Define (Function, "html", class {
333
443
  this.head.meta ({"http-equiv": "X-UA-Compatible", content: "IE=edge"});
334
444
  this.head.meta ({"http-equiv": "X-Cross-Origin", content: "*"});
335
445
  this.head.meta ({name: "viewport", content: this.var ["viewport"] || "width=device-width, initial-scale=1.0, maximum-scale=3.0, user-scalable=1"});
336
- 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)"});
337
447
  this.head.meta ({name: "author", content: this.var ["author"] || "Netizen"});
338
448
  this.head.meta ({name: "description", content: this.var.description});
339
449
  this.head.meta ({name: "keywords", content: this.var ["keyword"]});
@@ -456,36 +566,6 @@ Define (Function.html, "markup", class {
456
566
  * xxx://xxx.xxx.xxx/xxx
457
567
  */
458
568
 
459
- /**
460
- * xxx
461
- *
462
- * title
463
- * description
464
- * sub description
465
- *
466
- * xxx://xxx.xxx.xxx/xxx
467
- */
468
-
469
- /**
470
- * xxx
471
- *
472
- * title
473
- * description
474
- * sub description
475
- *
476
- * xxx://xxx.xxx.xxx/xxx
477
- */
478
-
479
- /**
480
- * xxx
481
- *
482
- * title
483
- * description
484
- * sub description
485
- *
486
- * xxx://xxx.xxx.xxx/xxx
487
- */
488
-
489
569
  Symbol.export = {
490
570
  define: Object.define,
491
571
  object: Object, array: Array, string: String, number: Number, function: Function,
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.1611",
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>"