mirror of https://github.com/Squidex/squidex.git
58 changed files with 554 additions and 353 deletions
@ -0,0 +1,92 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { Types } from './../'; |
|||
|
|||
describe('Types', () => { |
|||
it('should make string check', () => { |
|||
expect(Types.isString('')).toBeTruthy(); |
|||
expect(Types.isString('string')).toBeTruthy(); |
|||
|
|||
expect(Types.isString(false)).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make number check', () => { |
|||
expect(Types.isNumber(0)).toBeTruthy(); |
|||
expect(Types.isNumber(1)).toBeTruthy(); |
|||
|
|||
expect(Types.isNumber(NaN)).toBeFalsy(); |
|||
expect(Types.isNumber(Infinity)).toBeFalsy(); |
|||
expect(Types.isNumber(false)).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make boolean check', () => { |
|||
expect(Types.isBoolean(true)).toBeTruthy(); |
|||
expect(Types.isBoolean(false)).toBeTruthy(); |
|||
|
|||
expect(Types.isBoolean(0)).toBeFalsy(); |
|||
expect(Types.isBoolean(1)).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make number array check', () => { |
|||
expect(Types.isArrayOfNumber([])).toBeTruthy(); |
|||
expect(Types.isArrayOfNumber([0, 1])).toBeTruthy(); |
|||
|
|||
expect(Types.isArrayOfNumber(['0', 1])).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make string array check', () => { |
|||
expect(Types.isArrayOfString([])).toBeTruthy(); |
|||
expect(Types.isArrayOfString(['0', '1'])).toBeTruthy(); |
|||
|
|||
expect(Types.isArrayOfString(['0', 1])).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make array check', () => { |
|||
expect(Types.isArray([])).toBeTruthy(); |
|||
expect(Types.isArray([0])).toBeTruthy(); |
|||
|
|||
expect(Types.isArray({})).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make object check', () => { |
|||
expect(Types.isObject({})).toBeTruthy(); |
|||
expect(Types.isObject({ v: 1 })).toBeTruthy(); |
|||
|
|||
expect(Types.isObject([])).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make RegExp check', () => { |
|||
expect(Types.isRegExp(/[.*]/)).toBeTruthy(); |
|||
|
|||
expect(Types.isRegExp('/[.*]/')).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make Date check', () => { |
|||
expect(Types.isDate(new Date())).toBeTruthy(); |
|||
|
|||
expect(Types.isDate(new Date().getDate())).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make undefined check', () => { |
|||
expect(Types.isUndefined(undefined)).toBeTruthy(); |
|||
|
|||
expect(Types.isUndefined(null)).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make null check', () => { |
|||
expect(Types.isNull(null)).toBeTruthy(); |
|||
|
|||
expect(Types.isNull(undefined)).toBeFalsy(); |
|||
}); |
|||
|
|||
it('should make function check', () => { |
|||
expect(Types.isFunction(() => { /* NOOP */ })).toBeTruthy(); |
|||
|
|||
expect(Types.isFunction([])).toBeFalsy(); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,70 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
export module Types { |
|||
export function isString(value: any): boolean { |
|||
return typeof value === 'string' || value instanceof String; |
|||
} |
|||
|
|||
export function isNumber(value: any): boolean { |
|||
return typeof value === 'number' && isFinite(value); |
|||
} |
|||
|
|||
export function isArray(value: any): boolean { |
|||
return Array.isArray(value); |
|||
} |
|||
|
|||
export function isFunction(value: any): boolean { |
|||
return typeof value === 'function'; |
|||
} |
|||
|
|||
export function isObject(value: any): boolean { |
|||
return value && typeof value === 'object' && value.constructor === Object; |
|||
} |
|||
|
|||
export function isBoolean(value: any): boolean { |
|||
return typeof value === 'boolean'; |
|||
}; |
|||
|
|||
export function isNull(value: any): boolean { |
|||
return value === null; |
|||
} |
|||
|
|||
export function isUndefined(value: any): boolean { |
|||
return typeof value === 'undefined'; |
|||
} |
|||
|
|||
export function isRegExp(value: any): boolean { |
|||
return value && typeof value === 'object' && value.constructor === RegExp; |
|||
} |
|||
|
|||
export function isDate(value: any): boolean { |
|||
return value instanceof Date; |
|||
} |
|||
|
|||
export function isArrayOfNumber(value: any): boolean { |
|||
return isArrayOf(value, v => isNumber(v)); |
|||
} |
|||
|
|||
export function isArrayOfString(value: any): boolean { |
|||
return isArrayOf(value, v => isString(v)); |
|||
} |
|||
|
|||
export function isArrayOf(value: any, validator: (v: any) => boolean): boolean { |
|||
if (!Array.isArray(value)) { |
|||
return false; |
|||
} |
|||
|
|||
for (let v of value) { |
|||
if (!validator(v)) { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue