49 lines
3.0 KiB
JavaScript
49 lines
3.0 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
class Autocast {
|
|
constructor() {
|
|
this.commonStrings = {
|
|
'true': true,
|
|
'false': false,
|
|
'undefined': undefined,
|
|
'null': null,
|
|
'NaN': NaN
|
|
};
|
|
}
|
|
process(key, value, o) {
|
|
if (typeof (value) === 'object')
|
|
return;
|
|
o[key] = this._cast(value);
|
|
}
|
|
traverse(o, func) {
|
|
for (let i in o) {
|
|
func.apply(this, [i, o[i], o]);
|
|
if (o[i] !== null && typeof (o[i]) === 'object') {
|
|
this.traverse(o[i], func);
|
|
}
|
|
}
|
|
}
|
|
autocast(s) {
|
|
if (typeof (s) === 'object') {
|
|
this.traverse(s, this.process);
|
|
return s;
|
|
}
|
|
return this._cast(s);
|
|
}
|
|
_cast(s) {
|
|
let key;
|
|
if (s instanceof Date)
|
|
return s;
|
|
if (typeof s === 'boolean')
|
|
return s;
|
|
if (!isNaN(s))
|
|
return Number(s);
|
|
for (key in this.commonStrings) {
|
|
if (s === key)
|
|
return this.commonStrings[key];
|
|
}
|
|
return s;
|
|
}
|
|
}
|
|
exports.default = Autocast;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXV0b2Nhc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdXRpbHMvYXV0b2Nhc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxNQUFxQixRQUFRO0lBQTdCO1FBSUUsa0JBQWEsR0FBRztZQUNkLE1BQU0sRUFBRSxJQUFJO1lBQ1osT0FBTyxFQUFFLEtBQUs7WUFDZCxXQUFXLEVBQUUsU0FBUztZQUN0QixNQUFNLEVBQUUsSUFBSTtZQUNaLEtBQUssRUFBRSxHQUFHO1NBQ1gsQ0FBQTtJQStDSCxDQUFDO0lBN0NDLE9BQU8sQ0FBRS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