Skip to content

Commit

Permalink
Merge branch 'beta-v1.2.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Harvey committed Aug 25, 2020
2 parents 17c22b3 + 28013c9 commit 9385ad1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 58 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $ npm install @chharvey/requirejson

## Asynchronously
```js
const { requireJSON } = require('@chharvey/requirejson')
const {requireJSON} = require('@chharvey/requirejson')

requireJSON('./my-file.jsonld').then((my_json_object) => {
console.log(my_json_object['@type'])
Expand All @@ -19,9 +19,9 @@ requireJSON('./my-file.jsonld').then((my_json_object) => {

## Synchronously
```js
const { requireJSONSync } = require('@chharvey/requirejson')
const {requireJSONSync} = require('@chharvey/requirejson')

let my_json_object = requireJSONSync('./my-file.jsonld')
const my_json_object = requireJSONSync('./my-file.jsonld')
console.log(my_json_object['@type'])
```

Expand All @@ -34,14 +34,14 @@ import {
JSONObject,
JSONArray,
JSONPrimitive,
JSONSchema, // WARNING{DEPRECATED} - use `@types/json-schema` instead
JSONSchemaObject, // WARNING{DEPRECATED} - use `@types/json-schema` instead
JSONSchema, // WARNING: DEPRECATED - use `@types/json-schema` instead
JSONSchemaObject, // WARNING: DEPRECATED - use `@types/json-schema` instead
JSONLDDocument,
JSONLDObject,
} from '@chharvey/requirejson'

let my_json_object: Promise<JSONValue> = requireJSON('./my-file.jsonld')
let my_json_object_sync: JSONValue = requireJSONSync('./my-file.jsonld')
const my_json_object: Promise<JSONValue> = requireJSON('./my-file.jsonld')
const my_json_object_sync: JSONValue = requireJSONSync('./my-file.jsonld')
```

## Functions
Expand Down
29 changes: 14 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
const fs = require('fs')
const util = require('util')
const fs = require('fs')


module.exports.requireJSON = async function requireJSON(filepath) {
let data = await util.promisify(fs.readFile)(filepath, 'utf8')
module.exports.requireJSON = async (filepath) => {
const data = fs.promises.readFile(filepath, 'utf8')
let object;
try {
object = JSON.parse(data)
} catch (e) {
e.filename = filepath
console.error(e)
throw e
object = JSON.parse(await data)
} catch (err) {
err.filename = filepath
console.error(err)
throw err
}
return object
}


module.exports.requireJSONSync = function requireJSONSync(filepath) {
let data = fs.readFileSync(filepath, 'utf8')
module.exports.requireJSONSync = (filepath) => {
const data = fs.readFileSync(filepath, 'utf8')
let object;
try {
object = JSON.parse(data)
} catch (e) {
e.filename = filepath
console.error(e)
throw e
} catch (err) {
err.filename = filepath
console.error(err)
throw err
}
return object
}
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONValue } from './json.d'
import type {JSONValue} from './json.d'


/**
Expand All @@ -17,8 +17,8 @@ export declare function requireJSON(filepath: string): Promise<JSONValue>;
export declare function requireJSONSync(filepath: string): JSONValue;


export { JSONValue, JSONObject, JSONArray, JSONPrimitive } from './json.d'
export { JSONLDDocument, JSONLDObject } from './json-ld.d'
export {JSONValue, JSONObject, JSONArray, JSONPrimitive} from './json.d'
export {JSONLDDocument, JSONLDObject} from './json-ld.d'
export {
JSONSchema,
JSONSchemaObject,
Expand Down
46 changes: 25 additions & 21 deletions json-ld.d.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
import { JSONValue, JSONObject, JSONArray, JSONPrimitive } from './json.d'
import type {JSONObject, JSONPrimitive} from './json.d'


/**
* A single JSON-LD document.
* @see https://github.com/json-ld/json-ld.org/blob/1.0/schemas/jsonld-schema.json
*/
export interface JSONLDDocument extends JSONLDObject {
'@context'?: Context[]|Context|null;
'@graph'?: { [key: string]: JSONLDObject; }|JSONLDObject[];
[key: string]: (JSONLDObject|JSONPrimitive)[]|JSONLDObject|JSONPrimitive|undefined;
'@context'?: Context[] | Context | null;
'@graph'?: {
[key: string]: JSONLDObject;
} | JSONLDObject[];
[key: string]: (JSONLDObject | JSONPrimitive)[] | JSONLDObject | JSONPrimitive | undefined;
}

type Context = {
[key: string]: {
'@id': string;
'@type': '@id';
}|{
'@reverse': string;
}|string;
}|string

export interface JSONLDObject extends JSONObject {
/**
* @format uri
*/
'@base'?: string|null;
'@container'?: '@index'|'@list'|'@set'|null;
'@base'?: string | null;
'@container'?: '@index' | '@list' | '@set' | null;
/**
* @format uri
*/
'@id'?: string;
'@language'?: string|null;
'@language'?: string | null;
'@list'?: JSONLDObject[];
'@reverse'?: { [key: string]: JSONLDObject; }|string|null;
'@reverse'?: {
[key: string]: JSONLDObject;
} | string | null;
'@set'?: JSONLDObject[];
'@type'?: string[]|string|null;
'@value'?: string|number|boolean|null;
'@type'?: string[] | string | null;
'@value'?: string | number | boolean | null;
/**
* @format uri
*/
'@vocab'?: string|null;
[key: string]: (JSONLDObject|JSONPrimitive)[]|JSONLDObject|JSONPrimitive|undefined;
'@vocab'?: string | null;
[key: string]: (JSONLDObject | JSONPrimitive)[] | JSONLDObject | JSONPrimitive | undefined;
}

type Context = {
[key: string]: {
'@id': string;
'@type': '@id';
} | {
'@reverse': string;
} | string;
} | string
6 changes: 3 additions & 3 deletions json-schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// WARNING - this file is deprecated. you should import from `@types/json-schema` directly.


import { JSONSchema7Definition, JSONSchema7 } from 'json-schema'
import type {JSONSchema7Definition, JSONSchema7} from 'json-schema'

export {
JSONSchema7Definition,
JSONSchema7,
JSONSchema7Definition as JSONSchema, // WARNING{DEPRECATED}
JSONSchema7 as JSONSchemaObject, // WARNING{DEPRECATED}
JSONSchema7Definition as JSONSchema, // WARNING: DEPRECATED
JSONSchema7 as JSONSchemaObject, // WARNING: DEPRECATED
}
8 changes: 4 additions & 4 deletions json.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* Any JSON value.
*/
export type JSONValue = JSONObject|JSONArray|JSONPrimitive
export type JSONValue = JSONObject | JSONArray | JSONPrimitive

/**
* Any JSON object.
*/
export interface JSONObject {
[key: string]: JSONValue|undefined;
[key: string]: JSONValue | undefined;
}

/**
* Any JSON array.
*/
export interface JSONArray extends Array<JSONValue> {}
export type JSONArray = Array<JSONValue>

/**
* Any non-object, non-array JSON value.
*/
export type JSONPrimitive = string|number|boolean|null
export type JSONPrimitive = string | number | boolean | null
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chharvey/requirejson",
"version": "1.1.0",
"version": "1.2.0",
"description": "Require JSON files with a file extension other than `.json`.",
"main": "index.js",
"scripts": {
Expand All @@ -23,5 +23,8 @@
"homepage": "https://github.com/chharvey/requirejson#readme",
"dependencies": {
"@types/json-schema": "^7.0.2"
},
"peerDependencies": {
"typescript": "^3.8.0 || ~4.0.0"
}
}

0 comments on commit 9385ad1

Please sign in to comment.