Skip to content

Commit

Permalink
test: added tests for undefined conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
bconnorwhite committed Oct 4, 2020
1 parent 92ec342 commit f32ba0a
Show file tree
Hide file tree
Showing 9 changed files with 2,986 additions and 156 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 5 additions & 1 deletion .gitignore
@@ -1,2 +1,6 @@
build
/build
/coverage
node_modules
.DS_Store
.env
yarn-error.log
18 changes: 11 additions & 7 deletions README.md
@@ -1,13 +1,16 @@
<div align="center">
<h1>parse-json-object</h1>
<a href="https://npmjs.com/package/parse-json-object">
<img alt="npm" src="https://img.shields.io/npm/v/parse-json-object.svg">
<img alt="NPM" src="https://img.shields.io/npm/v/parse-json-object.svg">
</a>
<a href="https://github.com/bconnorwhite/parse-json-object">
<img alt="typescript" src="https://img.shields.io/github/languages/top/bconnorwhite/parse-json-object.svg">
<img alt="TypeScript" src="https://img.shields.io/github/languages/top/bconnorwhite/parse-json-object.svg">
</a>
<a href='https://coveralls.io/github/bconnorwhite/parse-json-object?branch=master'>
<img alt="Coverage Status" src="https://img.shields.io/coveralls/github/bconnorwhite/parse-json-object.svg?branch=master">
</a>
<a href="https://github.com/bconnorwhite/parse-json-object">
<img alt="GitHub stars" src="https://img.shields.io/github/stars/bconnorwhite/parse-json-object?label=Stars%20Appreciated%21&style=social">
<img alt="GitHub Stars" src="https://img.shields.io/github/stars/bconnorwhite/parse-json-object?label=Stars%20Appreciated%21&style=social">
</a>
<a href="https://twitter.com/bconnorwhite">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/bconnorwhite.svg?label=%40bconnorwhite&style=social">
Expand All @@ -23,11 +26,11 @@

## Installation

```bash
```sh
yarn add parse-json-object
```

```bash
```sh
npm install parse-json-object
```

Expand Down Expand Up @@ -81,10 +84,11 @@ interface JSONArray extends Array<JSONValue> {};

<h2>License <img align="right" alt="license" src="https://img.shields.io/npm/l/parse-json-object.svg"></h2>

[MIT](https://mit-license.org/)
[MIT](https://opensource.org/licenses/MIT)

<br />

## Related Packages:
- [stringify-json-object](https://www.npmjs.com/package/stringify-json-object): Stringify and format a JSON object.

- [stringify-json-object](https://www.npmjs.com/package/stringify-json-object): Stringify and format a JSON object
- [types-json](https://www.npmjs.com/package/types-json): Type checking for JSON objects
23 changes: 17 additions & 6 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "parse-json-object",
"version": "2.0.0",
"description": "Parse a typed JSON object.",
"description": "Parse a typed JSON object",
"license": "MIT",
"author": {
"name": "Connor White",
Expand All @@ -26,15 +26,26 @@
"main": "./build/index.js",
"scripts": {
"build": "bob build",
"postversion": "git push",
"prepublishOnly": "yarn build && yarn test",
"test": "jest"
"commit": "bob commit",
"lint": "bob lint",
"release": "bob publish",
"test": "bob test"
},
"dependencies": {
"types-json": "^1.2.0"
},
"devDependencies": {
"@bconnorwhite/bob": "^2.1.0",
"jest": "^26.4.2"
"@bconnorwhite/bob": "^2.9.2"
},
"eslintConfig": {
"extends": "eslint-config-bob"
},
"husky": {
"hooks": {
"commit-msg": "bob lint commit"
}
},
"npmpackagejsonlint": {
"extends": "npm-package-json-lint-config-bob"
}
}
2 changes: 1 addition & 1 deletion source/index.ts
Expand Up @@ -9,7 +9,7 @@ export function parse<T extends JSONValue>(text: string | undefined, isType: (va
} else {
return undefined;
}
} catch(e) {
} catch(err) {
return undefined;
}
} else {
Expand Down
25 changes: 0 additions & 25 deletions test/index.test.js

This file was deleted.

38 changes: 38 additions & 0 deletions test/index.test.ts
@@ -0,0 +1,38 @@
import { test, expect } from "@jest/globals";
import { parse, parseJSONValue, parseJSONObject, parseJSONArray, parseString, isJSONArray } from "../source";

test("parse invalid", () => {
expect(parse('{"ok":true}', isJSONArray)).toBe(undefined);
});

test("parseJSONValue object", () => {
expect(parseJSONValue('{"ok":true}')).toEqual({ ok: true });
});

test("parseJSONValue array", () => {
expect(parseJSONValue('[{"ok":true}]')).toEqual([{ ok: true }]);
});

test("parseJSONValue string", () => {
expect(parseJSONValue('"ok"')).toBe("ok");
});

test("parseJSONValue undefined", () => {
expect(parseJSONValue(undefined)).toBe(undefined);
});

test("parseJSONValue invalid", () => {
expect(parseJSONValue("ok")).toBe(undefined);
});

test("parseJSONObject", () => {
expect(parseJSONObject('{"ok":true}')).toEqual({ ok: true });
});

test("parseJSONArray", () => {
expect(parseJSONArray('[{"ok":true}]')).toEqual([{ ok: true }]);
});

test("parseString", () => {
expect(parseString('"ok"')).toBe("ok");
});
29 changes: 10 additions & 19 deletions tsconfig.json
@@ -1,26 +1,17 @@
{
"extends": "@bconnorwhite/bob",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"lib": [
"dom",
"esnext"
"rootDirs": [
"source",
"types"
],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"outDir": "build",
"removeComments": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
"typeRoots": [
"node_modules/@types",
"types"
]
},
"include": [
"source"
"source",
"types"
]
}

0 comments on commit f32ba0a

Please sign in to comment.