Skip to content

Commit

Permalink
Merge pull request #55 from teknologi-umum/language/json
Browse files Browse the repository at this point in the history
feat: json language support
  • Loading branch information
Reinaldy Rafli authored Oct 14, 2021
2 parents d649eae + cbc750e commit daf3a43
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { HTML } from './languages/html';
import { Java } from './languages/java';
import { Javascript } from './languages/javascript';
import { Julia } from './languages/julia';
import { JSON } from './languages/json';
import { Kotlin } from './languages/kotlin';
import { Lua } from './languages/lua';
import { Markdown } from './languages/markdown';
Expand All @@ -37,6 +38,7 @@ const languages: Record<string, LanguagePattern[]> = {
Java,
Javascript,
Julia,
JSON,
Kotlin,
Lua,
Markdown,
Expand Down
16 changes: 16 additions & 0 deletions src/languages/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { LanguagePattern } from '../types';

export const JSON: LanguagePattern[] = [
// object declaration on top
{ pattern: /^\{$/, type: 'meta.module', nearTop: true },
// normal data type
{ pattern: /^\s*".+":\s*(".+"|[0-9]+|null|true|false)(,)?$/, type: 'keyword' },
// object and array
{ pattern: /^\s*".+":\s*(\{|\[)$/, type: 'keyword' },
// inline key/value pair in object
// e.g { "id": 1, "body": "some comment", "postId": 1 }
{ pattern: /^\s*".+":\s*\{(\s*".+":\s*(".+"|[0-9]+|null|true|false)(,)?\s*){1,}\}(,)?$/, type: 'keyword' },
// inline value in array
// e.g "middlewares": ["./fixtures/middlewares/en", "./fixtures/middlewares/jp"]
{ pattern: /\s*".+"\s*\[\s*((".+"|[0-9]+|null|true|false)(,)?\s*){1,}\](,)?$/, type: 'keyword' },
];
1 change: 1 addition & 0 deletions tests/cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('hello world', () => {
Java: 0,
Javascript: 0,
Julia: 2,
JSON: 0,
Kotlin: 0,
Lua: 2,
Markdown: 0,
Expand Down
1 change: 1 addition & 0 deletions tests/cs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test('hello world', () => {
Java: -40,
Javascript: -40,
Julia: 5,
JSON: 0,
Kotlin: 0,
Lua: -20,
Markdown: 0,
Expand Down
123 changes: 123 additions & 0 deletions tests/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('1', () => {
const code = detectLang(`{
"key": "value",
"keys": "must always be enclosed in double quotes",
"numbers": 0,
"strings": "Hellø, wørld. All unicode is allowed, along with \\"escaping\\".",
"has bools?": true,
"nothingness": null,
"big number": 1.2e+100,
"objects": {
"comment": "Most of your structure will come from objects.",
"array": [0, 1, 2, 3, "Arrays can have anything in them.", 5],
"another object": {
"comment": "These things can be nested, very useful."
}
},
"silliness": [
{
"sources of potassium": ["bananas"]
},
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, "neo"],
[0, 0, 0, 1]
]
],
"alternative style": {
"comment": "check this out!"
, "comma position": "doesn't matter, if it's before the next key, it's valid"
, "another comment": "how nice"
},
"whitespace": "Does not matter.",
"that was short": "And done. You now know everything JSON has to offer."
}`);
assert.equal(code.language, 'JSON');
});

test('2', () => {
const code = detectLang(`{
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
}`);
assert.equal(code.language, 'JSON');
});

test('3', () => {
const code = detectLang(`{
"keywords": [
"JSON",
"server",
"fake",
"REST",
"API",
"prototyping",
"mock",
"mocking",
"test",
"testing",
"rest",
"data",
"dummy",
"sandbox"
]
}`);
assert.equal(code.language, 'JSON');
});

test('4', () => {
const code = detectLang(`{
"/api/": "/",
"/blog/:resource/:id/show": "/:resource/:id",
"/blog/:category": "/posts?category=:category"
}`);
assert.equal(code.language, 'JSON');
});

test('5', () => {
const code = detectLang(`{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}`);
assert.equal(code.language, 'JSON');
});

test('6', () => {
const code = detectLang(`{
"middlewares": ["./fixtures/middlewares/en", "./fixtures/middlewares/jp"]
}`);
assert.equal(code.language, 'JSON');
});

test.run();
1 change: 1 addition & 0 deletions tests/large.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ test('large input', () => {
Java: -832,
Javascript: -452,
Julia: 24,
JSON: 0,
Kotlin: 0,
Lua: -1820,
Markdown: 0,
Expand Down

0 comments on commit daf3a43

Please sign in to comment.