mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 05:58:26 +08:00
chore(util): add isPrimitive() util
This commit is contained in:
@ -2,6 +2,39 @@ import * as util from '../../../src/util';
|
||||
|
||||
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', () => {
|
||||
|
||||
it('should test a=undefined', () => {
|
||||
|
@ -111,6 +111,10 @@ export const isBlank = (val: any) => val === undefined || val === null;
|
||||
export const isObject = (val: any) => typeof val === 'object';
|
||||
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 {
|
||||
if (typeof val === 'string') {
|
||||
val = val.toLowerCase().trim();
|
||||
|
Reference in New Issue
Block a user