npm run format -ws
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
ExpressionData,
|
||||
ExpressionDataInterface,
|
||||
Kind,
|
||||
kindStr,
|
||||
} from "./expressiondata";
|
||||
import {ExpressionData, ExpressionDataInterface, Kind, kindStr} from "./expressiondata";
|
||||
|
||||
export class Array implements ExpressionDataInterface {
|
||||
private v: ExpressionData[] = [];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ExpressionDataInterface, Kind } from "./expressiondata";
|
||||
import {ExpressionDataInterface, Kind} from "./expressiondata";
|
||||
|
||||
export class BooleanData implements ExpressionDataInterface {
|
||||
constructor(public readonly value: boolean) {}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { StringData } from "./string";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {StringData} from "./string";
|
||||
|
||||
describe("dictionary", () => {
|
||||
it("pairs contains all values", () => {
|
||||
const d = new Dictionary();
|
||||
d.add("ABC", new StringData("val"));
|
||||
|
||||
expect(d.pairs()).toEqual([{ key: "ABC", value: new StringData("val") }]);
|
||||
expect(d.pairs()).toEqual([{key: "ABC", value: new StringData("val")}]);
|
||||
});
|
||||
|
||||
it("does not add duplicate entries", () => {
|
||||
@@ -14,6 +14,6 @@ describe("dictionary", () => {
|
||||
d.add("ABC", new StringData("val1"));
|
||||
d.add("abc", new StringData("val2"));
|
||||
|
||||
expect(d.pairs()).toEqual([{ key: "ABC", value: new StringData("val1") }]);
|
||||
expect(d.pairs()).toEqual([{key: "ABC", value: new StringData("val1")}]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import {
|
||||
ExpressionData,
|
||||
ExpressionDataInterface,
|
||||
Kind,
|
||||
kindStr,
|
||||
Pair,
|
||||
} from "./expressiondata";
|
||||
import {ExpressionData, ExpressionDataInterface, Kind, kindStr, Pair} from "./expressiondata";
|
||||
|
||||
export class Dictionary implements ExpressionDataInterface {
|
||||
private keys: string[] = [];
|
||||
private v: ExpressionData[] = [];
|
||||
private indexMap: { [key: string]: number } = {};
|
||||
private indexMap: {[key: string]: number} = {};
|
||||
|
||||
constructor(...pairs: Pair[]) {
|
||||
for (const p of pairs) {
|
||||
@@ -56,7 +50,7 @@ export class Dictionary implements ExpressionDataInterface {
|
||||
const result: Pair[] = [];
|
||||
|
||||
for (const key of this.keys) {
|
||||
result.push({ key, value: this.v[this.indexMap[key.toLowerCase()]] });
|
||||
result.push({key, value: this.v[this.indexMap[key.toLowerCase()]]});
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { Null } from "./null";
|
||||
import { Array } from "./array";
|
||||
import { StringData } from "./string";
|
||||
import { NumberData } from "./number";
|
||||
import { BooleanData } from "./boolean";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {Null} from "./null";
|
||||
import {Array} from "./array";
|
||||
import {StringData} from "./string";
|
||||
import {NumberData} from "./number";
|
||||
import {BooleanData} from "./boolean";
|
||||
|
||||
export enum Kind {
|
||||
String = 0,
|
||||
@@ -12,7 +12,7 @@ export enum Kind {
|
||||
Boolean,
|
||||
Number,
|
||||
CaseSensitiveDictionary,
|
||||
Null,
|
||||
Null
|
||||
}
|
||||
|
||||
export function kindStr(k: Kind): string {
|
||||
@@ -43,13 +43,7 @@ export interface ExpressionDataInterface {
|
||||
number(): number;
|
||||
}
|
||||
|
||||
export type ExpressionData =
|
||||
| Array
|
||||
| Dictionary
|
||||
| StringData
|
||||
| BooleanData
|
||||
| NumberData
|
||||
| Null;
|
||||
export type ExpressionData = Array | Dictionary | StringData | BooleanData | NumberData | Null;
|
||||
|
||||
export type Pair = {
|
||||
key: string;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export { Array } from "./array";
|
||||
export { BooleanData } from "./boolean";
|
||||
export { Dictionary } from "./dictionary";
|
||||
export { ExpressionData, Kind } from "./expressiondata";
|
||||
export { Null } from "./null";
|
||||
export { NumberData } from "./number";
|
||||
export { replacer } from "./replacer";
|
||||
export { reviver } from "./reviver";
|
||||
export { StringData } from "./string";
|
||||
export {Array} from "./array";
|
||||
export {BooleanData} from "./boolean";
|
||||
export {Dictionary} from "./dictionary";
|
||||
export {ExpressionData, Kind} from "./expressiondata";
|
||||
export {Null} from "./null";
|
||||
export {NumberData} from "./number";
|
||||
export {replacer} from "./replacer";
|
||||
export {reviver} from "./reviver";
|
||||
export {StringData} from "./string";
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
ExpressionData,
|
||||
ExpressionDataInterface,
|
||||
Kind,
|
||||
} from "./expressiondata";
|
||||
import {ExpressionData, ExpressionDataInterface, Kind} from "./expressiondata";
|
||||
|
||||
export class Null implements ExpressionDataInterface {
|
||||
constructor() {}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ExpressionDataInterface, Kind } from "./expressiondata";
|
||||
import {ExpressionDataInterface, Kind} from "./expressiondata";
|
||||
|
||||
export class NumberData implements ExpressionDataInterface {
|
||||
constructor(public readonly value: number) {}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Array } from "./array";
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { Null } from "./null";
|
||||
import { NumberData } from "./number";
|
||||
import { replacer } from "./replacer";
|
||||
import { StringData } from "./string";
|
||||
import {Array} from "./array";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {Null} from "./null";
|
||||
import {NumberData} from "./number";
|
||||
import {replacer} from "./replacer";
|
||||
import {StringData} from "./string";
|
||||
|
||||
describe("replacer", () => {
|
||||
it("null", () => {
|
||||
@@ -11,22 +11,14 @@ describe("replacer", () => {
|
||||
});
|
||||
|
||||
it("array", () => {
|
||||
expect(
|
||||
JSON.stringify(
|
||||
new Array(new StringData("a"), new StringData("b")),
|
||||
replacer,
|
||||
" "
|
||||
)
|
||||
).toEqual('[\n "a",\n "b"\n]');
|
||||
expect(JSON.stringify(new Array(new StringData("a"), new StringData("b")), replacer, " ")).toEqual(
|
||||
'[\n "a",\n "b"\n]'
|
||||
);
|
||||
});
|
||||
|
||||
it("dictionary", () => {
|
||||
expect(
|
||||
JSON.stringify(
|
||||
new Dictionary({ key: "a", value: new NumberData(42) }),
|
||||
replacer,
|
||||
" "
|
||||
)
|
||||
).toEqual('{\n "a": 42\n}');
|
||||
expect(JSON.stringify(new Dictionary({key: "a", value: new NumberData(42)}), replacer, " ")).toEqual(
|
||||
'{\n "a": 42\n}'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Array } from "./array";
|
||||
import { BooleanData } from "./boolean";
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { Null } from "./null";
|
||||
import { NumberData } from "./number";
|
||||
import { StringData } from "./string";
|
||||
import {Array} from "./array";
|
||||
import {BooleanData} from "./boolean";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {Null} from "./null";
|
||||
import {NumberData} from "./number";
|
||||
import {StringData} from "./string";
|
||||
|
||||
/**
|
||||
* Replacer can be passed to JSON.stringify to convert an ExpressionData object into plain JSON
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Array } from "./array";
|
||||
import { BooleanData } from "./boolean";
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { ExpressionData } from "./expressiondata";
|
||||
import { Null } from "./null";
|
||||
import { NumberData } from "./number";
|
||||
import { reviver } from "./reviver";
|
||||
import { StringData } from "./string";
|
||||
import {Array} from "./array";
|
||||
import {BooleanData} from "./boolean";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {ExpressionData} from "./expressiondata";
|
||||
import {Null} from "./null";
|
||||
import {NumberData} from "./number";
|
||||
import {reviver} from "./reviver";
|
||||
import {StringData} from "./string";
|
||||
|
||||
describe("reviver", () => {
|
||||
const tests: {
|
||||
@@ -16,32 +16,32 @@ describe("reviver", () => {
|
||||
{
|
||||
name: "null",
|
||||
data: "null",
|
||||
want: new Null(),
|
||||
want: new Null()
|
||||
},
|
||||
{
|
||||
name: "number",
|
||||
data: "1",
|
||||
want: new NumberData(1),
|
||||
want: new NumberData(1)
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
data: `"a"`,
|
||||
want: new StringData("a"),
|
||||
want: new StringData("a")
|
||||
},
|
||||
{
|
||||
name: "true boolean",
|
||||
data: "true",
|
||||
want: new BooleanData(true),
|
||||
want: new BooleanData(true)
|
||||
},
|
||||
{
|
||||
name: "false boolean",
|
||||
data: "false",
|
||||
want: new BooleanData(false),
|
||||
want: new BooleanData(false)
|
||||
},
|
||||
{
|
||||
name: "array",
|
||||
data: `[1,2,3]`,
|
||||
want: new Array(new NumberData(1), new NumberData(2), new NumberData(3)),
|
||||
want: new Array(new NumberData(1), new NumberData(2), new NumberData(3))
|
||||
},
|
||||
{
|
||||
name: "nested array",
|
||||
@@ -51,7 +51,7 @@ describe("reviver", () => {
|
||||
new NumberData(2),
|
||||
new Array(new NumberData(3), new NumberData(4)),
|
||||
new NumberData(5)
|
||||
),
|
||||
)
|
||||
},
|
||||
{
|
||||
name: "complex array",
|
||||
@@ -59,26 +59,23 @@ describe("reviver", () => {
|
||||
want: new Array(
|
||||
new Dictionary({
|
||||
key: "a",
|
||||
value: new Array(new BooleanData(true), new NumberData(2)),
|
||||
value: new Array(new BooleanData(true), new NumberData(2))
|
||||
}),
|
||||
new Dictionary({ key: "b", value: new StringData("three") }),
|
||||
new Dictionary({ key: "c", value: new Null() })
|
||||
),
|
||||
new Dictionary({key: "b", value: new StringData("three")}),
|
||||
new Dictionary({key: "c", value: new Null()})
|
||||
)
|
||||
},
|
||||
{
|
||||
name: "dictionary",
|
||||
data: `{ "object1": {} }`,
|
||||
want: new Dictionary({
|
||||
key: "object1",
|
||||
value: new Dictionary(),
|
||||
}),
|
||||
},
|
||||
value: new Dictionary()
|
||||
})
|
||||
}
|
||||
];
|
||||
|
||||
test.each(tests)(
|
||||
"$name",
|
||||
({ data, want }: { data: string; want: ExpressionData }) => {
|
||||
expect(JSON.parse(data, reviver)).toEqual(want);
|
||||
}
|
||||
);
|
||||
test.each(tests)("$name", ({data, want}: {data: string; want: ExpressionData}) => {
|
||||
expect(JSON.parse(data, reviver)).toEqual(want);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Array as dArray } from "./array";
|
||||
import { BooleanData } from "./boolean";
|
||||
import { Dictionary } from "./dictionary";
|
||||
import { ExpressionData, Pair } from "./expressiondata";
|
||||
import { Null } from "./null";
|
||||
import { NumberData } from "./number";
|
||||
import { StringData } from "./string";
|
||||
import {Array as dArray} from "./array";
|
||||
import {BooleanData} from "./boolean";
|
||||
import {Dictionary} from "./dictionary";
|
||||
import {ExpressionData, Pair} from "./expressiondata";
|
||||
import {Null} from "./null";
|
||||
import {NumberData} from "./number";
|
||||
import {StringData} from "./string";
|
||||
|
||||
/**
|
||||
* Reviver can be passed to `JSON.parse` to convert plain JSON into an `ExpressionData` object.
|
||||
@@ -35,10 +35,10 @@ export function reviver(key: string, val: any): ExpressionData {
|
||||
if (typeof val === "object") {
|
||||
return new Dictionary(
|
||||
...Object.keys(val).map(
|
||||
(k) =>
|
||||
k =>
|
||||
({
|
||||
key: k,
|
||||
value: val[k],
|
||||
value: val[k]
|
||||
} as Pair)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ExpressionDataInterface, Kind } from "./expressiondata";
|
||||
import {ExpressionDataInterface, Kind} from "./expressiondata";
|
||||
|
||||
export class StringData implements ExpressionDataInterface {
|
||||
constructor(public readonly value: string) {}
|
||||
|
||||
Reference in New Issue
Block a user