Skip to content

Commit d4a8f55

Browse files
committed
Add object to kv string serializer
1 parent eb71a9e commit d4a8f55

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

packages/kv/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { Token, TokenList, TokenType, Position, Literal, Range, Item, ParseError
33
//import { formatAll, FormattingOptions } from "./formatter";
44
import { isWhitespace, isQuoted, stripQuotes, isFloatValue, isIntegerValue, isScalarValue } from "./string-util";
55
import { parseText, parseTokens } from "./parser";
6+
import { serialize } from "./serializer"
67

78
export {
89
tokenize,
910
Token, TokenList, TokenType, Position, Literal as PositionedLiteral, Range, Item, ParseError, ParseErrorType, Document,
1011
//formatAll, FormattingOptions,
1112
isWhitespace, isQuoted, stripQuotes, isFloatValue, isIntegerValue, isScalarValue,
12-
parseText, parseTokens
13+
parseText, parseTokens,
14+
serialize
1315
};

packages/kv/src/serializer.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {serialize} from "./serializer";
2+
3+
test("Serialize Object simple", () => {
4+
const obj = {
5+
prop1: "string val",
6+
prop2: 10,
7+
boolProp: false,
8+
boolProp2: true,
9+
dateProp: new Date(2004, 10, 11, 8, 10, 0),
10+
};
11+
12+
const kvStr = serialize(obj);
13+
expect(kvStr).toBe(`"Object" {
14+
"prop1" "string val"
15+
"prop2" "10"
16+
"boolProp" "false"
17+
"boolProp2" "true"
18+
"dateProp" "Thu Nov 11 2004 08:10:00 GMT+0100 (Central European Standard Time)"
19+
}`);
20+
});
21+
22+
test("Serialize Object nested", () => {
23+
const obj = {
24+
prop1: "string val",
25+
sub: {
26+
more: 10,
27+
stuff: "but nested"
28+
}
29+
};
30+
31+
const kvStr = serialize(obj);
32+
expect(kvStr).toBe(`"Object" {
33+
"prop1" "string val"
34+
"sub" {
35+
"more" "10"
36+
"stuff" "but nested"
37+
}
38+
}`);
39+
});

packages/kv/src/serializer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export function serialize(obj: Object, options?: { name?: string, indentSpaces?: number, indentTabs?: boolean }, nestedLevel: number = 0): string {
2+
let indentObjStr: string = "";
3+
let indentStrKv: string = "";
4+
let indentChar = "";
5+
if(options?.indentTabs) {
6+
indentChar = "\t";
7+
} else {
8+
for (let i = 0; i < (options?.indentSpaces ?? 2); i++) {
9+
indentChar += " ";
10+
}
11+
}
12+
for (let i = 0; i < nestedLevel; i++) {
13+
indentObjStr += indentChar;
14+
}
15+
indentStrKv = indentObjStr + indentChar;
16+
17+
const kvObjHead = `${indentObjStr}"${options?.name ?? "Object"}" {`;
18+
const kvLines: string[] = [kvObjHead];
19+
for (const [key, val] of Object.entries(obj)) {
20+
const valIsObj = typeof val === "object";
21+
const objHasCustomToString = typeof val.toString === "function" && val.toString !== Object.prototype.toString;
22+
const doSerializeObj = valIsObj && !objHasCustomToString;
23+
24+
if(doSerializeObj) {
25+
const serialized = serialize(val, { ...options, name: key }, nestedLevel + 1);
26+
kvLines.push(serialized);
27+
} else {
28+
const kvString = `${indentStrKv}"${key}" "${String(val)}"`;
29+
kvLines.push(kvString);
30+
}
31+
}
32+
kvLines.push(`${indentObjStr}}`);
33+
34+
const fullStr = kvLines.join("\n");
35+
return fullStr;
36+
}

0 commit comments

Comments
 (0)