-
Notifications
You must be signed in to change notification settings - Fork 6
/
nimn.js
553 lines (490 loc) · 16.7 KB
/
nimn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.nimn = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var char = require("./common").char;
function BooleanType(fName, defaultVal){
this._name = fName;
this._type = DataType.BOOLEAN;
this._decodeChar = {};
this._decodeChar[char(175)] = null;
this._decodeChar[char(184)] = defaultVal;
this._decodeChar[char(181)] = true;
this._decodeChar[char(183)] = false;
}
BooleanType.prototype._encode = function(v){
if(v){
return chars.yes;
}else if(v === false){
return chars.no;
}else if(v === undefined){
return chars.missingPremitive;
}else if(v === null){
return chars.nilPremitive;
}
//return booleanValues[v];
};
BooleanType.prototype._decode = function(v,i){
return {
index: i+1,
value: this._decodeChar[ v[i] ]
}
};
module.exports = BooleanType;
},{"./common":2}],2:[function(require,module,exports){
exports.DataType = {
BOOLEAN : 1,
LIST : 2,
MAP : 3,
VARMAP : 4,
STRING : 5,
NUMBER : 6,
}
var char = function (a){
return String.fromCharCode(a);
}
exports.char = char;
exports.range = {
start : char(175),
end : char(188),
}
exports.chars= {
nilChar : char(176),
nilPremitive : char(175),
missingChar : char(186),
missingPremitive : char(184),
emptyChar : char(178),
emptyPremitive: char(177),//empty Premitive
boundaryChar : char(179),
fieldNameBoundaryChar : char(188),
objStart: char(182),
objEnd: char(180),
arrStart: char(187),
arrEnd: char(185),
yes: char(181),
no: char(183),
}
exports.startsWithNimnChar = function(str, type){
return type._type < 5 || ( str.length === 1 && inRange(str) );
}
//var nimnCharRegx = new RegExp("([\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB])", 'g');
//var nimnCharRegx = new RegExp("[^\]([\xAF-\xBB])", 'g');
/* function sanitize(v){
return v.replace(nimnCharRegx,'\\$1');
//return v;
} */
var inRange = function (v){
return v >= exports.range.start && v <= exports.range.end;
}
exports.inRange = inRange;
exports.read = function(str,from,to){
to = to || str.length;
for(;from < to;from++){
if( inRange(str[ from ]) && str[ from -1 ] !== "\\"){
return from;
}
}
return to;
}
exports.sanitize = function(str){
var newStr = "";
for(var i=0;i < str.length;i++){
if( inRange( str[ i] ) ){
newStr += `\\${str[ i]}`
}else{
newStr += str[ i];
}
}
return newStr;
}
exports.removeBackspace = function(str){
var newStr = "";
for(var i=0;i < str.length;i++){
if( str[ i ] === "\\" && inRange( str[ i +1 ] )){
continue;
}
newStr += str[ i ];
}
return newStr;
}
},{}],3:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var startsWithNimnChar = require("./common").startsWithNimnChar;
function ListType(fName){
this._name = fName;
this._type = DataType.LIST;
this._item = null;
}
ListType.prototype._encode = function (v){
if(v === undefined){
return chars.missingChar;
}else if(v === null){
return chars.nilChar;
}else if(v.length === 0){
return chars.emptyChar;
}else {
var len = v.length;
var str = this._item._encode( v[ 0 ] );
var wasNimnChar = startsWithNimnChar(str,this._item);
for(var i=1; i < len; i++){
var newStr = this._item._encode( v[ i ] );
var isNimnChar = startsWithNimnChar(newStr,this._item);
if( isNimnChar || wasNimnChar ){
str += newStr;
}else{
str += `${chars.boundaryChar}${newStr}`;
}
wasNimnChar = isNimnChar;
}
return `${chars.arrStart}${str}${chars.arrEnd}`;
}
};
ListType.prototype._decode = function(v,i){
if(v[i] === chars.emptyChar){
return {index: i+1, value: []};
}else if(v[i] === chars.missingChar){
return {index: i+1, value: undefined };
}else if(v[i] === chars.nilChar){
return {index: i+1, value: null }
}else if(v[i] === chars.arrStart){
i++;
var currentArr = [];
var str = this._item._decode( v,i );
i = str.index;
if(str.value !== undefined)
currentArr.push(str.value);
for( ; v[i] !== chars.arrEnd ;){
var itemVal;
if(v[i] === chars.boundaryChar){
itemVal = this._item._decode( v,i +1 );
}else{
itemVal = this._item._decode( v,i );
}
i = itemVal.index;
if(itemVal.value !== undefined)
currentArr.push(itemVal.value);
}
return {
index : i+1,
value: currentArr
}
}else{
throw Error("Invalid character at position " + i);
}
};
module.exports = ListType;
},{"./common":2}],4:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var startsWithNimnChar = require("./common").startsWithNimnChar;
function MapType(fName){
this._name = fName;
this._type = DataType.MAP;
this._keys = [];
this._len = 0;
}
MapType.prototype._encode = function(v){
if(v === undefined){
return chars.missingChar;
}else if(v === null){
return chars.nilChar;
}else if(Object.keys(v).length === 0){
return chars.emptyChar;
}else {
var str = this._keys[0]._encode( v[ this._keys[0]._name ] );
var wasNimnChar = startsWithNimnChar(str,this._keys[0]);
for(var i=1; i < this._len; i++){
var newStr = this._keys[i]._encode( v[ this._keys[i]._name ] );
var isNimnChar = startsWithNimnChar(newStr,this._keys[i]);
if( isNimnChar || wasNimnChar ){
str += newStr;
}else{
str += `${chars.boundaryChar}${newStr}`;
}
wasNimnChar = isNimnChar;
}
return `${chars.objStart}${str}${chars.objEnd}`;
}
};
MapType.prototype._decode = function(v,i){
if(v[i] === chars.emptyChar){
return {index: i+1, value: {} };
}else if(v[i] === chars.missingChar){
return {index: i+1, value: undefined };
}else if(v[i] === chars.nilChar){
return {index: i+1, value: null }
}else if(v[i] === chars.objStart){
i++;
var currentObj = {};
var str = this._keys[0]._decode( v,i );
i = str.index;
if(str.value !== undefined){
currentObj[ this._keys[ 0 ]._name ] = str.value;
}
for(var key_i=1; key_i < this._len && v[i] !== chars.objEnd; key_i++){
var keyVal ;
if(v[i] === chars.boundaryChar){
keyVal = this._keys[ key_i ]._decode( v,i +1 );
}else{
keyVal = this._keys[ key_i ]._decode( v,i );
}
i = keyVal.index;
if(keyVal.value !== undefined){
currentObj[ this._keys[ key_i ]._name ] = keyVal.value;
}
}
if(v[i] !== chars.objEnd){
i = skipUntilThisObjectEnds(v, i);
}
return {
index : i+1,
value: currentObj
}
}else{
throw Error("Invalid character at position " + i);
}
};
var skipUntilThisObjectEnds = function(str, from,to){
to = to || str.length;
var count = 0;
for(;from < to; from++){
if(chars.objStart === str[ from ]) {
count ++;
}else if( chars.objEnd === str[ from ] && str[ from -1 ] !== "\\" ){
if(count === 0) return from;
else count --;
}
}
return from;
}
module.exports = MapType;
},{"./common":2}],5:[function(require,module,exports){
/* var decodeNimnChar = {};
decodeNimnChar[char(175)] = null;
decodeNimnChar[char(184)] = undefined;
decodeNimnChar[char(177)] = "";
decodeNimnChar[char(181)] = true;
decodeNimnChar[char(183)] = false; */
/* var booleanValues = {};
booleanValues[undefined] = chars.missingPremitive;
booleanValues[null] = chars.nilPremitive;
booleanValues[true] = chars.yes;
booleanValues[false] = chars.no;
var stringValues = {};
stringValues[undefined] = chars.missingPremitive;
stringValues[null] = chars.nilPremitive;
stringValues[""] = chars.emptyPremitive;
var numberValues = {};
numberValues[undefined] = chars.missingPremitive;
numberValues[null] = chars.nilPremitive; */
var BooleanType = require("./booleanType");
var StringType = require("./stringType");
var NumberType = require("./numberType");
var MapType = require("./mapType");
var ListType = require("./listType");
var VarMapType = require("./varMapType");
function buildSchema(schema, shouldSanitize){
if(shouldSanitize !== false){
shouldSanitize = true;
}
if(schema.type === "map"){
var mapSchema = new MapType(schema.name);
mapSchema._keys = [];
mapSchema._len = schema.detail.length;
for(var i=0; i < mapSchema._len; i++){
mapSchema._keys.push( buildSchema(schema.detail[i], shouldSanitize ) );
}
return mapSchema;
}else if(schema.type === "list"){
var listSchema = new ListType(schema.name);
listSchema._item = buildSchema(schema.detail, shouldSanitize);
return listSchema;
}else if(schema.type === "varmap"){
var listSchema = new VarMapType(schema.name);
listSchema._item = buildSchema(schema.detail, shouldSanitize);
return listSchema;
}else if(schema.type === "boolean"){
return new BooleanType(schema.name, schema.default);
}else if(schema.type === "string"){
return new StringType(schema.name, schema.default, shouldSanitize);
}else{//number
return new NumberType(schema.name, schema.default);
}
}
function parse(schema, jsObj){
return schema._encode(jsObj);
}
function parseBack(schema, nimnData){
return schema._decode(nimnData,0).value;
}
exports.buildSchema = buildSchema;
exports.stringify = parse;
exports.parse = parseBack;
},{"./booleanType":1,"./listType":3,"./mapType":4,"./numberType":6,"./stringType":7,"./varMapType":8}],6:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var char = require("./common").char;
var inRange = require("./common").inRange;
var read = require("./common").read;
function NumberType(fName, defaultVal){
this._name = fName;
this._type = DataType.NUMBER;
this._decodeChar = {};
this._decodeChar[char(175)] = null;
this._decodeChar[char(184)] = defaultVal;
}
NumberType.prototype._encode = function(v){
if(v === undefined){
return chars.missingPremitive;
}else if(v === null){
return chars.nilPremitive;
}else {
return v;
}
//return numberValues[v] || v;
};
NumberType.prototype._decode = function(v,i){
if( inRange(v[i]) ){
return {
index: i+1,
value: this._decodeChar[ v[i] ]
}
}else{
var nextIndex = read(v,i);
var val = v.substring(i,nextIndex);
if(val.indexOf(".") === -1){
val = Number.parseInt(val);
}else{
val = Number.parseFloat(val);
}
return { index: nextIndex, value: val};
}
};
module.exports = NumberType;
},{"./common":2}],7:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var char = require("./common").char;
var sanitize = require("./common").sanitize;
var removeBackspace = require("./common").removeBackspace;
var inRange = require("./common").inRange;
var read = require("./common").read;
function StringType(fName, defaultVal, shouldSanitize){
this._name = fName;
this._type = DataType.STRING;
this._sanitize = shouldSanitize ? sanitize : doNothing;
this._removeBackspace = shouldSanitize ? removeBackspace : doNothing;
this._decodeChar = {};
this._decodeChar[char(175)] = null;
this._decodeChar[char(184)] = defaultVal;
this._decodeChar[char(177)] = "";
}
StringType.prototype._encode = function (v){
if(v){
v = "" + v;
return this._sanitize(v);
}else if(v === undefined){
return chars.missingPremitive;
}else if(v === null){
return chars.nilPremitive;
}else if(v === ""){
return chars.emptyPremitive;
}else{
v = "" + v;
return this._sanitize(v);
}
};
StringType.prototype._decode = function(v,i){
if( inRange(v[i])){
return {
index: i+1,
value: this._decodeChar[ v[i] ]
}
}else{
var nextIndex = read(v,i);
return { index: nextIndex, value: this._removeBackspace( v.substring(i,nextIndex) )};
}
};
function doNothing(a){
return a;
}
module.exports = StringType;
},{"./common":2}],8:[function(require,module,exports){
var DataType = require("./common").DataType;
var chars = require("./common").chars;
var startsWithNimnChar = require("./common").startsWithNimnChar;
var read = require("./common").read;
function VarMapType(fName){
this._name = fName;
this._type = DataType.VARMAP;
this._item = null;
}
VarMapType.prototype._encode = function (v){
if(v === undefined){
return chars.missingChar;
}else if(v === null){
return chars.nilChar;
}else if(Object.keys(v).length === 0){
return chars.emptyChar;
}else {
var keys = Object.keys(v);
var len = keys.length;
var str = this._item._encode( v[ keys[0] ] );
var wasNimnChar = startsWithNimnChar(str,this._item);
str = keys[0] + chars.fieldNameBoundaryChar + str;
for(var i=1; i < len; i++){
var newStr = this._item._encode( v[ keys[i] ] );
var isNimnChar = startsWithNimnChar(newStr,this._item);
newStr = keys[i] + chars.fieldNameBoundaryChar + newStr;
if( wasNimnChar ){
str += newStr;
}else{
str += `${chars.boundaryChar}${newStr}`;
}
wasNimnChar = isNimnChar;
}
return `${chars.arrStart}${str}${chars.arrEnd}`;
}
};
VarMapType.prototype._decode = function(v,i){
if(v[i] === chars.emptyChar){
return {index: i+1, value: {}};
}else if(v[i] === chars.missingChar){
return {index: i+1, value: undefined };
}else if(v[i] === chars.nilChar){
return {index: i+1, value: null }
}else if(v[i] === chars.arrStart){
i++;
var currentObj = {};
//get the index of first chars.fieldNameBoundaryChar after i
var indexOfFieldSeparator = read(v,i);
var fieldName = v.substring( i, indexOfFieldSeparator );
i = indexOfFieldSeparator + 1;
var str = this._item._decode( v,i );
i = str.index;
if(str.value !== undefined)
currentObj[fieldName] = str.value;
for( ; v[i] !== chars.arrEnd ;){
var itemVal;
if(v[i] === chars.boundaryChar){
i++;
}
var indexOfFieldSeparator = read(v,i);
var fieldName = v.substring( i, indexOfFieldSeparator );
i = indexOfFieldSeparator + 1;
itemVal = this._item._decode( v,i );
i = itemVal.index;
if(itemVal.value !== undefined)
currentObj[fieldName] = itemVal.value;
}
return {
index : i+1,
value: currentObj
}
}else{
throw Error("Invalid character at position " + i);
}
};
module.exports = VarMapType;
},{"./common":2}]},{},[5])(5)
});