Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pure JSON code #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ var keyboard = kle.Serial.deserialize([
{ name: "Sample", author: "Your Name" },
["Q", "W", "E", "R", "T", "Y"]
]);

// or

var keyboard = kle.Serial.parse(`[
{ name: "Sample", author: "Your Name" },
["Q", "W", "E", "R", "T", "Y"]
]`);
```

## API
Expand All @@ -59,16 +52,6 @@ kle.Serial.deserialize(rows: Array<any>): Keyboard
object.
- The first entry is optionally a keyboard metadata object.

```ts
kle.Serial.parse(json5: string): Keyboard
```

- This function takes a JSON5-formatted string, parses it, then deserializes the
result into a `Keyboard` object.
- [JSON5](https://json5.org/) is a simplified / lenient version of JSON that is
easier for humans to type; in particular, it doesn't require quotes around
property names. Any valid JSON string should also be a valid JSON5 string.

### Keyboard Objects

```ts
Expand Down
8 changes: 1 addition & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as JSON5 from "json5";

export class Key {
color: string = "#cccccc";
labels: string[] = [];
Expand Down Expand Up @@ -92,7 +90,7 @@ export module Serial {
}

function deserializeError(msg, data?) {
throw "Error: " + msg + (data ? ":\n " + JSON5.stringify(data) : "");
throw "Error: " + msg + (data ? ":\n " + JSON.stringify(data) : "");
}

export function deserialize(rows: Array<any>): Keyboard {
Expand Down Expand Up @@ -204,8 +202,4 @@ export module Serial {
}
return kbd;
}

export function parse(json: string): Keyboard {
return deserialize(JSON5.parse(json));
}
}
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@
"mocha": "^6.1.4",
"mocha-lcov-reporter": "^1.3.0",
"typescript": "^3.5.3"
},
"dependencies": {
"json5": "^2.1.0"
}
}
27 changes: 0 additions & 27 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,31 +449,4 @@ describe("deserialization", function() {
expect(result.keys[2].textSize[6]).to.equal("2");
});
});

describe("of strings", function() {
it("should be lenient about quotes", function() {
var result1 = () =>
kbd.Serial.parse(`[
{ name: "Sample", author: "Your Name" },
["Q", "W", "E", "R", "T", "Y"]
]`);

var result2 = () =>
kbd.Serial.parse(`[
{ "name": "Sample", "author": "Your Name" },
["Q", "W", "E", "R", "T", "Y"]
]`);

var result3 = () =>
kbd.Serial.deserialize([
{ name: "Sample", author: "Your Name" },
["Q", "W", "E", "R", "T", "Y"]
]);

expect(result1).to.not.throw();
expect(result2).to.not.throw();
expect(result1(), "1<>2").to.deep.equal(result2());
expect(result1(), "1<>3").to.deep.equal(result3());
});
});
});