Skip to content

Commit

Permalink
feat(repository): add null type
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 29, 2020
1 parent 6f90360 commit 0aa585a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
39 changes: 39 additions & 0 deletions packages/repository/src/__tests__/unit/type/type.unit.ts
Expand Up @@ -349,6 +349,45 @@ describe('types', () => {
});
});

describe('null', () => {
const nullType = new types.NullType();
it('checks isInstance', () => {
expect(nullType.isInstance('str')).to.be.false();
expect(nullType.isInstance(null)).to.be.true();
expect(nullType.isInstance(undefined)).to.be.false();
expect(nullType.isInstance(true)).to.be.false();
expect(nullType.isInstance(false)).to.be.false();
expect(nullType.isInstance({x: 1})).to.be.false();
expect(nullType.isInstance([1, 2])).to.be.false();
expect(nullType.isInstance(1)).to.be.false();
expect(nullType.isInstance(new Date())).to.be.false();
});

it('checks isCoercible', () => {
expect(nullType.isCoercible('str')).to.be.false();
expect(nullType.isCoercible(null)).to.be.true();
expect(nullType.isCoercible(undefined)).to.be.true();
expect(nullType.isCoercible(true)).to.be.false();
expect(nullType.isCoercible({x: 1})).to.be.false();
expect(nullType.isCoercible(1)).to.be.false();
expect(nullType.isCoercible(new Date())).to.be.false();
});

it('creates defaultValue', () => {
expect(nullType.defaultValue()).to.be.null();
});

it('coerces values', () => {
expect(nullType.coerce(null)).to.null();
expect(nullType.coerce(undefined)).to.null();
});

it('serializes values', () => {
expect(nullType.serialize(null)).null();
expect(nullType.serialize(undefined)).null();
});
});

describe('array', () => {
const stringType = new types.StringType();
const arrayType = new types.ArrayType(stringType);
Expand Down
8 changes: 8 additions & 0 deletions packages/repository/src/type-resolver.ts
Expand Up @@ -51,6 +51,13 @@ export function isTypeResolver<T extends object>(
return true;
}

/**
* A boxed type for `null`
*/
export function Null() {
return null;
}

/**
* Check if the provided function is a built-in type provided by JavaScript
* and/or Node.js. E.g. `Number`, `Array`, `Buffer`, etc.
Expand All @@ -67,6 +74,7 @@ export function isBuiltinType(fn: Function): boolean {
fn === Date ||
fn === RegExp ||
fn === Buffer ||
fn === Null ||
// function as a type
fn === Function
);
Expand Down
19 changes: 11 additions & 8 deletions packages/repository/src/types/index.ts
Expand Up @@ -15,17 +15,18 @@
* - ArrayType: Array<T>
* - UnionType: Union of types
*/
import {Type} from './type';
import {StringType} from './string';
import {BooleanType} from './boolean';
import {NumberType} from './number';
import {DateType} from './date';
import {BufferType} from './buffer';
import {AnyType} from './any';
import {ArrayType} from './array';
import {UnionType} from './union';
import {ObjectType} from './object';
import {BooleanType} from './boolean';
import {BufferType} from './buffer';
import {DateType} from './date';
import {ModelType} from './model';
import {NullType} from './null';
import {NumberType} from './number';
import {ObjectType} from './object';
import {StringType} from './string';
import {Type} from './type';
import {UnionType} from './union';

export {
Type,
Expand All @@ -38,6 +39,7 @@ export {
ArrayType,
UnionType,
ModelType,
NullType,
ObjectType,
};

Expand All @@ -47,3 +49,4 @@ export const NUMBER = new NumberType();
export const DATE = new DateType();
export const BUFFER = new BufferType();
export const ANY = new AnyType();
export const NULL = new NullType();
35 changes: 35 additions & 0 deletions packages/repository/src/types/null.ts
@@ -0,0 +1,35 @@
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Type} from './type';

/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* Null type
*/
export class NullType implements Type<null> {
readonly name = 'boolean';

isInstance(value: any) {
return value === null;
}

defaultValue() {
return null;
}

isCoercible(value: any): boolean {
return value == null;
}

coerce(value: any) {
return null;
}

serialize(value: boolean | null | undefined) {
return null;
}
}

0 comments on commit 0aa585a

Please sign in to comment.