Skip to content

Commit

Permalink
Adds Undefined type
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcisbee committed Mar 10, 2020
1 parent 040d993 commit cf0722c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 21 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
scope: '@marcisbee'
- run: npm ci
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GH_TOKEN}}
# publish-gpr:
# needs: build
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-node@v1
# with:
# node-version: 12
# registry-url: https://npm.pkg.github.com/
# scope: '@marcisbee'
# - run: npm ci
# - run: npm run build
# - run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{secrets.GH_TOKEN}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "letype",
"version": "1.0.3",
"version": "1.1.0",
"description": "Type checker for any data structures",
"main": "dist/letype.js",
"module": "dist/letype.js",
Expand Down
2 changes: 1 addition & 1 deletion src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import typeCheck from './typeCheck';
*/
export default function assert(value, ...typeList) {
if (typeList && typeList.length === 1) {
if (!typeCheck(value, typeList[0])) {
if (!typeCheck(value, typeList[0], [], true)) {
throw new TypeError('Type checker found some type mismatches!');
}
} else
Expand Down
2 changes: 1 addition & 1 deletion src/assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('assert', () => {
assert('value', String);

expect(mockedTypeCheck).toHaveBeenCalledTimes(1);
expect(mockedTypeCheck).toHaveBeenCalledWith('value', String);
expect(mockedTypeCheck).toHaveBeenCalledWith('value', String, [], true);
});

test('should call `typeCheck()` once if multiple types are present, first valid', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/typeCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import CustomType from './types/Custom';
* @param {boolean} silent
* @returns {boolean}
*/
export default function typeCheck(value, type, path = [], silent = false) {
if (typeof type === 'function' && type.constructor instanceof CustomType) {
const { parse } = type();
export default function typeCheck(value, type, path = [], silent = true) {
if (typeof type === 'function' && type.prototype instanceof CustomType) {
// @ts-ignore
// eslint-disable-next-line new-cap
const { parse } = new type();
if (typeof parse === 'function') return parse(value);
}

Expand Down
27 changes: 27 additions & 0 deletions src/typeCheck.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/* eslint-disable no-new-func */
import typeCheck from './typeCheck';
import CustomType from './types/Custom';

class MockAny extends CustomType {
parse() {
return true;
}
}

// eslint-disable-next-line no-console
console.error = jest.fn();
Expand All @@ -13,6 +20,26 @@ describe('typeCheck', () => {
expect(typeCheck instanceof Function).toBe(true);
});

describe('CustomType', () => {
test.only('should call `parse` method on `CustomType`', () => {
MockAny.prototype.parse = jest.fn();
typeCheck('CustomValue', MockAny);

expect(MockAny.prototype.parse).toHaveBeenCalledTimes(1);
expect(MockAny.prototype.parse).toHaveBeenCalledWith('CustomValue');
});

test.only('should return output of parse method as `true`', () => {
MockAny.prototype.parse = jest.fn().mockImplementation(() => true);
expect(typeCheck('CustomValue', MockAny)).toBe(true);
});

test.only('should return output of parse method as `false`', () => {
MockAny.prototype.parse = jest.fn().mockImplementation(() => false);
expect(typeCheck('CustomValue', MockAny)).toBe(false);
});
});

describe('String', () => {
test.each([
'Value',
Expand Down
10 changes: 10 additions & 0 deletions src/types/Undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import CustomType from './Custom';

export default class Undefined extends CustomType {
/**
* @param {any} value
*/
parse(value) {
return typeof value === 'undefined';
}
}
49 changes: 49 additions & 0 deletions src/types/Undefined.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import CustomType from './Custom';
import Undefined from './Undefined';

describe('Undefined', () => {
afterEach(() => {
jest.resetAllMocks();
});

test('should extend `CustomType`', () => {
expect(Undefined.prototype instanceof CustomType).toBe(true);
});

test('should return `true` with value ``', () => {
// @ts-ignore
const output = new Undefined().parse();

expect(output).toBe(true);
});

test('should return `true` with value `undefined`', () => {
const output = new Undefined().parse(undefined);

expect(output).toBe(true);
});

test('should return `false` with value "1"', () => {
const output = new Undefined().parse('1');

expect(output).toBe(false);
});

test('should return `false` with value `0`', () => {
const output = new Undefined().parse(0);

expect(output).toBe(false);
});

test('should return `false` with value `null`', () => {
const output = new Undefined().parse(null);

expect(output).toBe(false);
});

test('should return `false` with value `NaN`', () => {
const output = new Undefined().parse(NaN);

expect(output).toBe(false);
});
});
2 changes: 2 additions & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Any from './Any';
import Or from './Or';
import Custom from './Custom';
import Undefined from './Undefined';

const types = {
Any,
Or,
Custom,
Undefined,
};

export default types;

0 comments on commit cf0722c

Please sign in to comment.