Skip to content

Commit 8737992

Browse files
committed
Types and optional quotes
Not the best implementation possible, but i didn't want to modify the module structure
1 parent 2ccc288 commit 8737992

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module "mojangson" {
2+
interface MojangsonEntry {
3+
value: object;
4+
type: string;
5+
}
6+
function simplify(data: object): object;
7+
function stringify({ value, type }: MojangsonEntry, quotes?: boolean): string;
8+
function parse(text: string): object;
9+
function normalize(str: string, quotes?: boolean): string;
10+
}

index.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const nearley = require('nearley')
22
const grammar = require('./grammar')
33

4-
function simplify (data) {
5-
function transform (value, type) {
4+
function simplify(data) {
5+
function transform(value, type) {
66
if (type === 'compound') {
77
return Object.keys(value).reduce(function (acc, key) {
88
acc[key] = simplify(value[key])
@@ -17,53 +17,59 @@ function simplify (data) {
1717
return transform(data.value, data.type)
1818
}
1919

20-
function stringify ({ value, type }) {
20+
function stringify({ value, type }, quotes = false) {
2121
if (type === 'compound') {
2222
const str = []
2323
const entries = Object.entries(value)
2424
for (let i = 0; i < entries.length; i++) {
2525
const _type = entries[i][0]
26-
let _value = stringify(entries[i][1])
27-
if (_type === 'string') _value = normalizeString(_value)
28-
str.push(`${_type}:${_value}`)
26+
let _value = stringify(entries[i][1], quotes)
27+
if (_type === 'string') _value = normalizeString(_value, quotes)
28+
if (quotes)
29+
str.push(`"${_type}":${_value}`)
30+
else
31+
str.push(`${_type}:${_value}`)
32+
2933
}
3034
return `{${str.join(',')}}`
3135
} else if (type === 'list') {
3236
if (!Array.isArray(value.value)) return '[]'
33-
const arrayElements = getArrayValues(value)
37+
const arrayElements = getArrayValues(value, quotes)
3438
return `[${arrayElements}]`
3539
} else if (type === 'byteArray' || type === 'intArray' || type === 'longArray') {
3640
const prefix = getArrayPrefix(type)
37-
const arrayElements = getArrayValues(value)
41+
const arrayElements = getArrayValues(value, quotes)
3842
return `[${prefix}${arrayElements}]`
3943
}
4044
let str = value + getSuffix(value, type)
41-
if (type === 'string') str = normalizeString(str)
45+
if (type === 'string') str = normalizeString(str, quotes)
4246
return str
4347
}
4448

45-
function normalizeString (str) {
49+
function normalizeString(str, quotes) {
4650
str = str.replace(/"/g, '\\"')
4751
if (/'|{|}|\[|\]|:|;|,|\(|\)|§|=/g.test(str) || str === '') str = `"${str}"`
52+
if (quotes && !str.startsWith('"'))
53+
str = `"${str}"`
4854
return str
4955
}
5056

51-
function getArrayValues ({ value: arr, type }) {
57+
function getArrayValues({ value: arr, type }, quotes) {
5258
const hasMissingEl = hasMissingElements(arr)
5359
const str = []
5460
// add nullable length that way [] is pased as []
5561
for (let i = 0; i < arr.length; i++) {
5662
let curr = arr[i]
5763
if (curr !== undefined) {
58-
curr = stringify({ value: curr, type })
64+
curr = stringify({ value: curr, type }, quotes)
5965
if (hasMissingEl) str.push(`${i}:${curr}`)
6066
else str.push(curr)
6167
}
6268
}
6369
return str.join(',')
6470
}
6571

66-
function hasMissingElements (arr) {
72+
function hasMissingElements(arr) {
6773
for (let i = 0; i < arr.length; i++) {
6874
if (arr[i] === undefined) return true
6975
}
@@ -72,7 +78,7 @@ function hasMissingElements (arr) {
7278

7379
const getArrayPrefix = type => type.charAt(0).toUpperCase() + ';'
7480

75-
function getSuffix (val, type) {
81+
function getSuffix(val, type) {
7682
if (type === 'double') return ((val >> 0) === val) ? 'd' : ''
7783
return { int: '', byte: 'b', short: 's', float: 'f', long: 'l', string: '' }[type]
7884
}
@@ -83,11 +89,11 @@ function getSuffix (val, type) {
8389
* @returns {string} the normalized mojangson
8490
*/
8591

86-
function normalize (str) {
87-
return stringify(parse(str))
92+
function normalize(str, quotes = false) {
93+
return stringify(parse(str), quotes)
8894
}
8995

90-
function parse (text) {
96+
function parse(text) {
9197
try {
9298
const parserNE = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
9399
parserNE.feed(text)
@@ -103,4 +109,4 @@ module.exports = {
103109
simplify,
104110
stringify,
105111
normalize
106-
}
112+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.0.2",
44
"description": "A mojangson parser written in node.js",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"repository": {
78
"type": "git",
89
"url": "git://github.com/rom1504/node-mojangson.git"

0 commit comments

Comments
 (0)