From e87ac2acdfb1d91355f81f24a0da476e718db237 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Wed, 4 May 2022 19:32:10 +0300 Subject: [PATCH 1/8] Types and optional quotes Not the best implementation possible, but i didn't want to modify the module structure --- index.d.ts | 10 ++++++++++ index.js | 25 +++++++++++++------------ package.json | 1 + 3 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6b2a09b --- /dev/null +++ b/index.d.ts @@ -0,0 +1,10 @@ +declare module "mojangson" { + interface MojangsonEntry { + value: object; + type: string; + } + function simplify(data: object): object; + function stringify({ value, type }: MojangsonEntry, quotes?: boolean): string; + function parse(text: string): object; + function normalize(str: string, quotes?: boolean): string; +} \ No newline at end of file diff --git a/index.js b/index.js index 6763422..7f2f8e7 100644 --- a/index.js +++ b/index.js @@ -17,45 +17,46 @@ function simplify (data) { return transform(data.value, data.type) } -function stringify ({ value, type }) { +function stringify ({ value, type }, quotes = false) { if (type === 'compound') { const str = [] const entries = Object.entries(value) for (let i = 0; i < entries.length; i++) { const _type = entries[i][0] - let _value = stringify(entries[i][1]) - if (_type === 'string') _value = normalizeString(_value) - str.push(`${_type}:${_value}`) + let _value = stringify(entries[i][1], quotes) + if (_type === 'string') _value = normalizeString(_value, quotes) + if (quotes) { str.push(`"${_type}":${_value}`) } else { str.push(`${_type}:${_value}`) } } return `{${str.join(',')}}` } else if (type === 'list') { if (!Array.isArray(value.value)) return '[]' - const arrayElements = getArrayValues(value) + const arrayElements = getArrayValues(value, quotes) return `[${arrayElements}]` } else if (type === 'byteArray' || type === 'intArray' || type === 'longArray') { const prefix = getArrayPrefix(type) - const arrayElements = getArrayValues(value) + const arrayElements = getArrayValues(value, quotes) return `[${prefix}${arrayElements}]` } let str = value + getSuffix(value, type) - if (type === 'string') str = normalizeString(str) + if (type === 'string') str = normalizeString(str, quotes) return str } -function normalizeString (str) { +function normalizeString (str, quotes) { str = str.replace(/"/g, '\\"') if (/'|{|}|\[|\]|:|;|,|\(|\)|§|=/g.test(str) || str === '') str = `"${str}"` + if (quotes && !str.startsWith('"')) { str = `"${str}"` } return str } -function getArrayValues ({ value: arr, type }) { +function getArrayValues ({ value: arr, type }, quotes) { const hasMissingEl = hasMissingElements(arr) const str = [] // add nullable length that way [] is pased as [] for (let i = 0; i < arr.length; i++) { let curr = arr[i] if (curr !== undefined) { - curr = stringify({ value: curr, type }) + curr = stringify({ value: curr, type }, quotes) if (hasMissingEl) str.push(`${i}:${curr}`) else str.push(curr) } @@ -83,8 +84,8 @@ function getSuffix (val, type) { * @returns {string} the normalized mojangson */ -function normalize (str) { - return stringify(parse(str)) +function normalize (str, quotes = false) { + return stringify(parse(str), quotes) } function parse (text) { diff --git a/package.json b/package.json index 10a457b..f507f43 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.0.2", "description": "A mojangson parser written in node.js", "main": "index.js", + "types": "index.d.ts", "repository": { "type": "git", "url": "git://github.com/rom1504/node-mojangson.git" From 2cd4d4b2a00dac6d627e645fbe6a9cb7736b55a8 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Thu, 5 May 2022 13:57:52 +0300 Subject: [PATCH 2/8] Improve types --- index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6b2a09b..8ab1691 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,10 @@ declare module "mojangson" { interface MojangsonEntry { - value: object; + value: any; type: string; } - function simplify(data: object): object; + function simplify(data: MojangsonEntry): any; function stringify({ value, type }: MojangsonEntry, quotes?: boolean): string; - function parse(text: string): object; + function parse(text: string): MojangsonEntry; function normalize(str: string, quotes?: boolean): string; } \ No newline at end of file From 974318bb8919b207cf5fb1020ef843d743aba0f0 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Sat, 14 May 2022 13:58:21 +0300 Subject: [PATCH 3/8] Version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f507f43..0b41004 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mojangson", - "version": "2.0.2", + "version": "2.0.3", "description": "A mojangson parser written in node.js", "main": "index.js", "types": "index.d.ts", From b58b50ac7fc62500a81e723a7a95be741ee0c9de Mon Sep 17 00:00:00 2001 From: Foresteam Date: Tue, 17 May 2022 00:02:48 +0300 Subject: [PATCH 4/8] Force quotes tests --- test/quotes.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/quotes.test.js diff --git a/test/quotes.test.js b/test/quotes.test.js new file mode 100644 index 0000000..051d717 --- /dev/null +++ b/test/quotes.test.js @@ -0,0 +1,18 @@ +/* eslint-env mocha */ +const assert = require('assert') +const mojangson = require('../') + +describe('Always use quotes for names and strings', () => { + // Seems that from a certain MC version SNBTs stopped working without quotes (as for 1.18.2) + [ + '{"var":"value1","var1":123b,"var2":"value3","var3":"value4"}', + '[B;1b,2b,3b]', + '{"id":"minecraft:yellow_shulker_box","Count":1b,"tag":{"BlockEntityTag":{"CustomName":"Stacked Totems","x":0,"y":0,"z":0,"id":"minecraft:shulker_box","Lock":""},"display":{"Name":"Stacked Totems"}},"Damage":0s}', + '{"SomeField":"кириллиц А"}', + '{"AField":"\\""}' + ].forEach(s => { + it('sould be unchanged', () => { + assert.strictEqual(mojangson.stringify(mojangson.parse(s), true), s) + }) + }) +}) From b9b76c02c0c47fde2670f4dc93656b6be85d4633 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Tue, 17 May 2022 00:40:42 +0300 Subject: [PATCH 5/8] Force quotes formatting tests --- test/quotes.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/quotes.test.js b/test/quotes.test.js index 051d717..26c6b1c 100644 --- a/test/quotes.test.js +++ b/test/quotes.test.js @@ -11,8 +11,18 @@ describe('Always use quotes for names and strings', () => { '{"SomeField":"кириллиц А"}', '{"AField":"\\""}' ].forEach(s => { - it('sould be unchanged', () => { + it('sould remain unchanged', () => { assert.strictEqual(mojangson.stringify(mojangson.parse(s), true), s) }) + }); + // Formatting tests + [ + ['{var: value1, var1:123b, "var2":value3, "var3": "value4"}', '{"var":"value1","var1":123b,"var2":"value3","var3":"value4"}'], + ['{SomeField: кириллицА}', '{"SomeField":"кириллицА"}'], + ['{AField: "\\""}', '{"AField":"\\""}'] + ].forEach(([input, expected]) => { + it('sould be equal', () => { + assert.strictEqual(mojangson.stringify(mojangson.parse(input), true), expected) + }) }) }) From 58def10da82878796ca8126125b61521c4c26b18 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Tue, 17 May 2022 01:09:11 +0300 Subject: [PATCH 6/8] .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3b4f5af..05c4f98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules grammar.js -package-lock.json \ No newline at end of file +package-lock.json +.pnpm-debug.log \ No newline at end of file From 6ab35a4b6ecee9e0e2cb1fd8b35d998bd307b225 Mon Sep 17 00:00:00 2001 From: Foresteam Date: Wed, 15 Mar 2023 23:27:03 +0300 Subject: [PATCH 7/8] Improve typings --- index.d.ts | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8ab1691..0c63a31 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,47 @@ -declare module "mojangson" { - interface MojangsonEntry { - value: any; - type: string; +declare module "@foresteam/mojangson" { + type MojangsonNumericType = 'byte' | 'short' | 'long' | 'float' | 'double' | 'int'; + type MojangsonArrayElementType = 'byte' | 'int' | 'long'; + type MojangsonElementaryType = 'string' | 'boolean' | MojangsonNumericType; + type MojangsonArrayType = 'byteArray' | 'intArray' | 'longArray'; + type MojangsonAnyType = 'compound' | MojangsonElementaryType | MojangsonArrayType; + type MojangsonAnyValue = MojangsonEntry | string | boolean | number; + + interface MojangsonNumber { + value: number; + type: 'byte' | 'short' | 'long' | 'float' | 'double' | 'int'; + }; + interface MojangsonBoolean { + value: boolean; + type: 'boolean'; + }; + interface MojangsonArray { + value: { + value: number[]; + type: 'byte' | 'int' | 'long'; + }; + type: 'byteArray' | 'intArray' | 'longArray'; + }; + interface MojangsonList { + value: { + type: MojangsonAnyType; + value: MojangsonAnyValue[]; + }; + type: 'list'; + }; + interface MojangsonString { + value: string; + type: 'string'; + }; + interface MojangsonCompound { + value: { + [key: string]: MojangsonEntry; + }; + type: 'compound'; } + type MojangsonEntry = MojangsonArray | MojangsonList | MojangsonString | MojangsonNumber | MojangsonBoolean | MojangsonCompound; + function simplify(data: MojangsonEntry): any; function stringify({ value, type }: MojangsonEntry, quotes?: boolean): string; function parse(text: string): MojangsonEntry; function normalize(str: string, quotes?: boolean): string; -} \ No newline at end of file +} From b1f14f86bec40e91b4d669950ce49d9ab24e226f Mon Sep 17 00:00:00 2001 From: Foresteam Date: Fri, 17 Mar 2023 21:26:19 +0300 Subject: [PATCH 8/8] Fix module name For the original package... --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 0c63a31..c6ae478 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -declare module "@foresteam/mojangson" { +declare module "mojangson" { type MojangsonNumericType = 'byte' | 'short' | 'long' | 'float' | 'double' | 'int'; type MojangsonArrayElementType = 'byte' | 'int' | 'long'; type MojangsonElementaryType = 'string' | 'boolean' | MojangsonNumericType;