chore(util): add isPrimitive() util

This commit is contained in:
Adam Bradley
2016-07-11 23:01:40 -05:00
parent 91f66d8972
commit 2e570837df
2 changed files with 37 additions and 0 deletions

View File

@ -2,6 +2,39 @@ import * as util from '../../../src/util';
export function run() { export function run() {
describe('isPrimitive', () => {
it('should be false for array/object values', () => {
expect(util.isPrimitive({})).toEqual(false);
expect(util.isPrimitive(new Date())).toEqual(false);
expect(util.isPrimitive([])).toEqual(false);
});
it('should be false for blank values', () => {
expect(util.isPrimitive(NaN)).toEqual(false);
expect(util.isPrimitive(null)).toEqual(false);
expect(util.isPrimitive(undefined)).toEqual(false);
});
it('should be true for number', () => {
expect(util.isPrimitive(-1)).toEqual(true);
expect(util.isPrimitive(0)).toEqual(true);
expect(util.isPrimitive(1)).toEqual(true);
});
it('should be true for boolean', () => {
expect(util.isPrimitive(true)).toEqual(true);
expect(util.isPrimitive(false)).toEqual(true);
});
it('should be true for string', () => {
expect(util.isPrimitive('')).toEqual(true);
expect(util.isPrimitive(' ')).toEqual(true);
expect(util.isPrimitive('hi')).toEqual(true);
});
});
describe('isCheckedProperty', () => { describe('isCheckedProperty', () => {
it('should test a=undefined', () => { it('should test a=undefined', () => {

View File

@ -111,6 +111,10 @@ export const isBlank = (val: any) => val === undefined || val === null;
export const isObject = (val: any) => typeof val === 'object'; export const isObject = (val: any) => typeof val === 'object';
export const isArray = Array.isArray; export const isArray = Array.isArray;
export const isPrimitive = function(val: any) {
return isString(val) || isBoolean(val) || (isNumber(val) && !isNaN(val));
};
export const isTrueProperty = function(val: any): boolean { export const isTrueProperty = function(val: any): boolean {
if (typeof val === 'string') { if (typeof val === 'string') {
val = val.toLowerCase().trim(); val = val.toLowerCase().trim();