Skip to content

Commit ba6c979

Browse files
committed
feat: Add TypeScript typings
1 parent 4a8f2d9 commit ba6c979

File tree

8 files changed

+243
-5
lines changed

8 files changed

+243
-5
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ test/fails
55
test/passes
66
test/recursive
77
test/v8
8+
test/typings.test.js
89
web/*.min.*

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
.DS_Store
12
.nyc_output
23
benchmarks/jison/*.js
34
benchmarks/pegjs/*.js
45
coverage
56
node_modules
67
lib/jsonlint*.js
78
lib/schema-drafts.js
9+
test/typings.test.js
810
web/*.min.*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ coverage
1212
scripts
1313
src
1414
test
15+
tslint.yml
1516
web/*.html

lib/jsonlint.d.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
type ParseMode = 'json' | 'cjson' | 'json5'
2+
3+
interface ParseOptions {
4+
ignoreComments?: boolean
5+
ignoreTrailingCommas?: boolean
6+
allowSingleQuotedStrings?: boolean
7+
allowDuplicateObjectKeys?: boolean
8+
mode?: ParseMode
9+
reviver?: Function
10+
}
11+
12+
/**
13+
* Parses a string formatted as JSON. It is compatible with the native
14+
* `JSON.parse` method.
15+
*
16+
* @example
17+
* ```ts
18+
* import { parse } from '@prantlf/jsonlint'
19+
* const parsed = parse('string with JSON data')
20+
* ```
21+
*
22+
* @param input - a string input to parse
23+
* @param reviverOrOptions - either a value reviver or an object
24+
* with multiple options
25+
* @returns the parsed result - a primitive value, array or object
26+
*/
27+
declare function parse (input: string, reviverOrOptions?: Function | ParseOptions): object
28+
29+
declare module '@prantlf/jsonlint/lib/validator' {
30+
type Environment = 'json-schema-draft-04' | 'json-schema-draft-06' | 'json-schema-draft-07'
31+
32+
interface CompileOptions {
33+
ignoreComments?: boolean
34+
ignoreTrailingCommas?: boolean
35+
allowSingleQuotedStrings?: boolean
36+
allowDuplicateObjectKeys?: boolean
37+
environment?: Environment
38+
mode?: ParseMode
39+
}
40+
41+
/**
42+
* Generates a JSON Schema validator.
43+
*
44+
* @example
45+
* ```ts
46+
* import { compile } from '@prantlf/jsonlint/lib/validator'
47+
* const validate = compile('string with JSON schema')
48+
* const parsed = validate('string with JSON data')
49+
* ```
50+
*
51+
* @param schema - a string with the JSON Schema to validate with
52+
* @param environmentOrOptions - either a string with the version
53+
* of the JSON Schema standard or an object
54+
* with multiple options
55+
* @returns the validator function
56+
*/
57+
function compile (schema: string, environmentOrOptions?: Environment | CompileOptions): Function
58+
}
59+
60+
export { parse }

package-lock.json

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"preferGlobal": true,
2727
"main": "lib/jsonlint.js",
28+
"typings": "lib/jsonlint.d.ts",
2829
"bin": {
2930
"jsonlint": "lib/cli.js"
3031
},
@@ -33,11 +34,13 @@
3334
},
3435
"scripts": {
3536
"prepare": "npm run lint && npm run build",
36-
"lint": "standard --fix -v",
37-
"build": "node scripts/bundle-jsonlint && uglifyjs -o web/jsonlint.min.js --source-map \"filename='jsonlint.js',url='jsonlint.min.js.map',includeSources=true\" lib/jsonlint.js && uglifyjs -o web/validator.min.js --source-map \"filename='validator.js',url='validator.min.js.map',includeSources=true\" lib/validator.js && uglifyjs -o web/formatter.min.js --source-map \"filename='formatter.js',url='formatter.min.js.map',includeSources=true\" lib/formatter.js && uglifyjs -o web/sorter.min.js --source-map \"filename='sorter.js',url='sorter.min.js.map',includeSources=true\" lib/sorter.js && node scripts/bundle-schema-drafts && uglifyjs -o web/schema-drafts.min.js --source-map \"filename='schema-drafts.js',url='schema-drafts.min.js.map',includeSources=true\" lib/schema-drafts.js && uglifyjs -o web/ajv.min.js --source-map \"filename='ajv.js',url='ajv.min.js.map',includeSources=true\" node_modules/ajv/dist/ajv.bundle.js",
38-
"test": "nyc --silent node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -S test/passes/strings.txt && nyc --silent --no-clean node lib/cli -M json5 test/passes/json5.text && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -pc test/fails/10.json || nyc report",
39-
"test:old": "node test/parse1 && node test/parse1 --native-parser && node test/parse2 && node test/parse3 && node test/parse4 && node test/parse5 && node test/portable && node lib/cli package.json test/recursive && node lib/cli -sq test/passes/hasOwnProperty.json && node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && node lib/cli -C test/passes/comments.txt && node lib/cli -S test/passes/strings.txt && node lib/cli -M json5 test/passes/json5.text && node lib/cli -v && node lib/cli -h",
40-
"coverage": "cat coverage/lcov.info | npx coveralls",
37+
"lint": "npm run lint:js && npm run lint:ts",
38+
"lint:js": "standard --fix -v",
39+
"lint:ts": "tslint --fix -t stylish lib/*.ts test/*.ts",
40+
"build": "npm run compile && npm run compile:tests",
41+
"compile": "node scripts/bundle-jsonlint && uglifyjs -o web/jsonlint.min.js --source-map \"filename='jsonlint.js',url='jsonlint.min.js.map',includeSources=true\" lib/jsonlint.js && uglifyjs -o web/validator.min.js --source-map \"filename='validator.js',url='validator.min.js.map',includeSources=true\" lib/validator.js && uglifyjs -o web/formatter.min.js --source-map \"filename='formatter.js',url='formatter.min.js.map',includeSources=true\" lib/formatter.js && uglifyjs -o web/sorter.min.js --source-map \"filename='sorter.js',url='sorter.min.js.map',includeSources=true\" lib/sorter.js && node scripts/bundle-schema-drafts && uglifyjs -o web/schema-drafts.min.js --source-map \"filename='schema-drafts.js',url='schema-drafts.min.js.map',includeSources=true\" lib/schema-drafts.js && uglifyjs -o web/ajv.min.js --source-map \"filename='ajv.js',url='ajv.min.js.map',includeSources=true\" node_modules/ajv/dist/ajv.bundle.js",
42+
"compile:tests": "tsc --lib es6 test/typings.test.ts",
43+
"test": "nyc --silent node test/typings.test.js && nyc --silent --no-clean node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -S test/passes/strings.txt && nyc --silent --no-clean node lib/cli -M json5 test/passes/json5.text && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -pc test/fails/10.json || nyc report",
4144
"start": "http-server -c 5",
4245
"web": "npm run web:sync && npm run web:deploy",
4346
"web:clone": "test ! -d ../jsonlint-pages && git clone --single-branch --branch gh-pages `git remote get-url origin` ../jsonlint-pages",
@@ -67,11 +70,15 @@
6770
"commander": "4.0.1"
6871
},
6972
"devDependencies": {
73+
"@types/node": "12.12.21",
7074
"http-server": "0.12.0",
7175
"js-yaml": "3.13.1",
7276
"nyc": "14.1.1",
7377
"standard": "14.3.1",
7478
"test": "0.6.0",
79+
"tslint": "5.20.1",
80+
"tslint-config-standard": "9.0.0",
81+
"typescript": "3.7.4",
7582
"uglify-js": "3.7.2"
7683
}
7784
}

test/typings.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { strict as assert } from 'assert'
2+
import { parse } from '..'
3+
import { compile } from '../lib/validator'
4+
5+
exports['test type declarations for parse'] = () => {
6+
const result = parse('{}')
7+
assert.equal(typeof result, 'object')
8+
parse('{}', () => undefined)
9+
parse('{}', {})
10+
parse('{}', { mode: 'json' })
11+
parse('{}', { mode: 'cjson' })
12+
parse('{}', { mode: 'json5' })
13+
parse('{}', {
14+
ignoreComments: true,
15+
ignoreTrailingCommas: true,
16+
allowSingleQuotedStrings: true,
17+
allowDuplicateObjectKeys: true,
18+
reviver: () => undefined
19+
})
20+
assert.ok(true)
21+
}
22+
23+
exports['test type declarations for compile'] = () => {
24+
const validate = compile('{}')
25+
assert.equal(typeof validate, 'function')
26+
compile('{}', 'json-schema-draft-04')
27+
compile('{}', 'json-schema-draft-06')
28+
compile('{}', 'json-schema-draft-07')
29+
compile('{}', {})
30+
compile('{}', { mode: 'json' })
31+
compile('{}', { mode: 'cjson' })
32+
compile('{}', { mode: 'json5' })
33+
compile('{}', { environment: 'json-schema-draft-04' })
34+
compile('{}', { environment: 'json-schema-draft-06' })
35+
compile('{}', { environment: 'json-schema-draft-07' })
36+
compile('{}', {
37+
ignoreComments: true,
38+
ignoreTrailingCommas: true,
39+
allowSingleQuotedStrings: true,
40+
allowDuplicateObjectKeys: true
41+
})
42+
assert.ok(true)
43+
}
44+
45+
if (require.main === module) { require('test').run(exports) }

tslint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extends: tslint-config-standard

0 commit comments

Comments
 (0)