web-atoms-core
Version:
169 lines • 8.59 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@web-atoms/unit-test/dist/Assert", "@web-atoms/unit-test/dist/Test", "../../services/JsonService", "../../unit/AtomTest"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Assert_1 = require("@web-atoms/unit-test/dist/Assert");
var Test_1 = require("@web-atoms/unit-test/dist/Test");
var JsonService_1 = require("../../services/JsonService");
var AtomTest_1 = require("../../unit/AtomTest");
var JsonServiceTest = /** @class */ (function (_super) {
__extends(JsonServiceTest, _super);
function JsonServiceTest() {
return _super !== null && _super.apply(this, arguments) || this;
}
JsonServiceTest.prototype.parse = function () {
// nothing..
var t = "{ \"date1\": \"/Date(1530518926876)/\", \"date2\": \"2018-07-02T08:09:48.045Z\" }";
var s = new JsonService_1.JsonService();
var v = s.parse(t);
Assert_1.default.isTrue(v.date1 instanceof Date);
Assert_1.default.isTrue(v.date2 instanceof Date);
};
JsonServiceTest.prototype.stringify = function () {
var s = new JsonService_1.JsonService();
var j = s.stringify({
key: "value",
_$_handlers: [
"h1", "h2"
]
});
var p = s.parse(j);
Assert_1.default.isUndefined(p._$_handlers);
};
JsonServiceTest.prototype.underscoreNamingStrategy = function () {
var s = new JsonService_1.JsonService();
// s.options.namingStrategy = "underscore";
var r = s.parse("{\n \"string_value\": \"text\",\n \"array_value\": [1, {\n \"string_value\": \"text\",\n \"object_value\": {\n \"int_value\": 10,\n \"bool_value\": false\n }\n }]\n }", { namingStrategy: "underscore" });
Assert_1.default.equals("text", r.stringValue);
Assert_1.default.equals(2, r.arrayValue.length);
var av = r.arrayValue[1];
Assert_1.default.equals("text", av.stringValue);
Assert_1.default.equals(10, av.objectValue.intValue);
Assert_1.default.equals(false, av.objectValue.boolValue);
};
JsonServiceTest.prototype.hyphenNamingStrategy = function () {
var s = new JsonService_1.JsonService();
s.options.namingStrategy = "hyphen";
var r = s.parse("{\n \"string-value\": \"text\",\n \"array-value\": [1, {\n \"string-value\": \"text\",\n \"object-value\": {\n \"int-value\": 10,\n \"bool-value\": false\n }\n }]\n }");
Assert_1.default.equals("text", r.stringValue);
Assert_1.default.equals(2, r.arrayValue.length);
var av = r.arrayValue[1];
Assert_1.default.equals("text", av.stringValue);
Assert_1.default.equals(10, av.objectValue.intValue);
Assert_1.default.equals(false, av.objectValue.boolValue);
};
JsonServiceTest.prototype.stringifyHyphenNamingStrategy = function () {
var s = new JsonService_1.JsonService();
s.options.namingStrategy = "hyphen";
var p = new JsonService_1.JsonService();
var a = s.stringify({
stringValue: "text",
objectValue: {
boolValue: false
},
arrayLike: {
length: 1,
1: 1
}
});
var o = p.parse(a);
Assert_1.default.equals("text", o["string-value"]);
};
JsonServiceTest.prototype.stringifyUnderscoreNamingStrategy = function () {
var s = new JsonService_1.JsonService();
s.options.namingStrategy = "underscore";
var p = new JsonService_1.JsonService();
var a = s.stringify({
stringValue: "text",
objectValue: {
boolValue: false
}
});
var o = p.parse(a);
// tslint:disable-next-line:no-string-literal
Assert_1.default.equals("text", o["string_value"]);
};
JsonServiceTest.prototype.dateParser = function () {
var s = new JsonService_1.JsonService();
var text = "\"2018-10-24T18:54:51.831Z\"";
var d = s.parse(text);
Assert_1.default.isTrue(d instanceof Date);
Assert_1.default.equals("2018-10-24T18:54:51.831Z", d.toISOString());
Assert_1.default.equals("{\"d\":\"2018-10-24T18:54:51.831Z\"}", s.stringify({ d: d }, { indent: 0 }));
};
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "parse", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "stringify", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "underscoreNamingStrategy", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "hyphenNamingStrategy", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "stringifyHyphenNamingStrategy", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "stringifyUnderscoreNamingStrategy", null);
__decorate([
Test_1.default,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], JsonServiceTest.prototype, "dateParser", null);
return JsonServiceTest;
}(AtomTest_1.AtomTest));
exports.JsonServiceTest = JsonServiceTest;
});
//# sourceMappingURL=JsonServiceTest.js.map