Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: isDate, isSymbol Verify Javascript dates & primitive Symbols #FewMoreGemToTheCrown #30

Merged
merged 6 commits into from Sep 17, 2019
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## To Be Released

* New APIs added:
* `t(dateObj).isDate` - is available.
* `t(Symbol('desc')).isSymbol` - is available

### version (undecided)

* what's next
Expand Down
38 changes: 36 additions & 2 deletions README.md
Expand Up @@ -64,6 +64,9 @@ t({}).isObject // => true
t([]).isArray // => true
t([]).isObject // => false

const sym = Symbol('typyIsAwesome');
t(sym).isSymbol // => true

// obj.goodKey.nestedKey = 'helloworld'
t(obj, 'goodKey.nestedKey').isDefined // => true
t(obj, 'badKey.nestedKey').isDefined // => false
Expand Down Expand Up @@ -108,6 +111,8 @@ const myObj = t(deepObj, 'badKey.goodKey').safeObject; // => undefined
- [isArray](#isarray)
- [isEmptyArray](#isemptyarray)
- [isFunction](#isfunction)
- [isDate](#isdate)
- [isSymbol](#issymbol)
- [safeObject](#safeobject)
- [safeObjectOrEmpty](#safeobjectorempty)
- [safeString](#safestring)
Expand Down Expand Up @@ -345,6 +350,32 @@ t(func).isFunction // => true
t({}).isFunction // => false
```

#### isDate

Returns _true_ if the input is a javascript's date object.

```js
const date = new Date();
t(date).isDate // => true
t({}).isDate // => false
```

#### isSymbol

Returns _true_ if the input is a javascript's Symbol.

```js
const mySym = Symbol(123);
const anotherSymbol = Symbol('typyIsAwesome');

t(mySym).isSymbol // => true;
t(Object(anotherSymbol)).isSymbol // => true;

t({}).isSymbol // => false
t([]).isSymbol // => false
t(null).isSymbol // => false
```

#### safeObject

Safely returns the value from a nested object path without throwing any error.
Expand Down Expand Up @@ -497,7 +528,8 @@ const superheroSchema = {
title: Schema.String,
alias: Schema.String,
}
]
],
lastSeen: Schema.Date
};
const batmanObject = {
name: 'Batman',
Expand All @@ -508,7 +540,8 @@ const batmanObject = {
title: 'The Dark Knight',
alias: 'Bruce',
}
]
],
lastSeen: new Date(14894561568)
};
const isSchemaValid = t(batmanObject, superheroSchema).isValid; // true

Expand All @@ -532,6 +565,7 @@ The following **Schema types** are available in typy.
- Null
- Undefined
- Function
- Date

#### addCustomTypes (Custom Types)

Expand Down
24 changes: 22 additions & 2 deletions examples/example.js
Expand Up @@ -6,7 +6,8 @@ const exampleObj = {
goodKey: 'I exist :)',
nestedKey: {
nestedGoodKey: 'Me too!'
}
},
symbolKey: Symbol(123)
};

console.log(`exampleObj is defined - ${t(exampleObj).isDefined}`); // true
Expand Down Expand Up @@ -42,12 +43,31 @@ const arr = [];
console.log(`arr is an Array - ${t(arr).isArray}`); // true
console.log(`arr is an empty Array - ${t(arr).isEmptyArray}`); // true

const date = new Date();
console.log(`type of 'date' is Date - ${t(date).isDate}`); // true
console.log(`type of 'obj' is not date - ${t(obj).isDate}`); // false
console.log(`type of 'arr' is not date - ${t(arr).isDate}`); // false

const exampleDateObj = {
key: {
nestedGoodDate: 'new Date()'
}
};
console.log(`exampleDateObj.key.nestedGoodDate is defined - ${t(exampleDateObj, 'key.nestedGoodDate').isDefined}`); // true
console.log(`exampleDateObj.key.nestedGoodDate is of type Date - ${t(exampleDateObj, 'key.nestedGoodDate').isDate}`); // true

const mySymbol = Symbol('symbol-description');
console.log(`is 'mySymbol' a Symbol - ${t(mySymbol).isSymbol}`);
console.log(`is 'obj' a Symbol? - ${t(obj).isSymbol}`);
console.log(`is 'arr' a Symbol? - ${t(arr).isSymbol}`);

// Schema Checks
const validSchema = {
goodKey: Schema.String,
nestedKey: {
nestedGoodKey: Schema.String
}
},
symbolKey: Schema.Symbol
};
console.log(`exampleObj matches validSchema - ${t(exampleObj, validSchema).isValid}`); // true

Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Expand Up @@ -25,7 +25,9 @@ declare module "typy" {
readonly isNumber: boolean
readonly isArray: boolean
readonly isEmptyArray: boolean
readonly isFunction: boolean
readonly isFunction: boolean
readonly isDate: boolean
readonly isSymbol: boolean
readonly safeObject: any
readonly safeObjectOrEmpty: any
readonly safeString: string
Expand Down
34 changes: 29 additions & 5 deletions src/typy.js
Expand Up @@ -9,8 +9,10 @@ class Typy {
Undefined: undefined,
Array: [],
/* istanbul ignore next */
Function: () => {}
}
Function: () => {},
Date: new Date(),
Symbol: Symbol('')
};

t = (obj, options) => {
this.input = obj;
Expand All @@ -35,8 +37,14 @@ class Typy {
};

get isValid() {
if (this.schemaCheck !== null && this.schemaCheck === true
&& this.input !== null && this.input !== undefined) { return true; }
if (
this.schemaCheck !== null &&
this.schemaCheck === true &&
this.input !== null &&
this.input !== undefined
) {
return true;
}
return false;
}

Expand Down Expand Up @@ -89,7 +97,8 @@ class Typy {
if (
typeof this.input === 'object' &&
this.input === Object(this.input) &&
Object.prototype.toString.call(this.input) !== '[object Array]'
Object.prototype.toString.call(this.input) !== '[object Array]' &&
Object.prototype.toString.call(this.input) !== '[object Date]'
) {
return true;
}
Expand Down Expand Up @@ -131,6 +140,21 @@ class Typy {
return false;
}

get isDate() {
return (
this.input instanceof Date ||
Object.prototype.toString.call(this.input) === '[object Date]'
);
}

get isSymbol() {
return (
typeof this.input === 'symbol' ||
(typeof this.input === 'object' &&
Object.prototype.toString.call(this.input) === '[object Symbol]')
);
}

get safeObject() {
return this.input;
}
Expand Down
24 changes: 22 additions & 2 deletions test/typy.test.js
Expand Up @@ -189,6 +189,22 @@ describe('Typy', () => {
const func = () => {};
expect(t(func).isFunction === true).toBeTruthy();
});

test('should test if type is Date', () => {
const date = new Date();
expect(t(date).isDate === true).toBeTruthy();
});

test('should test if type is Symbol', () => {
const mySymbol = Symbol('someSymbol');
expect(t(mySymbol).isSymbol === true).toBeTruthy();
expect(t(Object(mySymbol)).isSymbol === true).toBeTruthy();

expect(t('Hello World').isSymbol === false).toBeTruthy();
expect(t({}).isSymbol === false).toBeTruthy();
expect(t([]).isSymbol === false).toBeTruthy();
expect(t(23).isSymbol === false).toBeTruthy();
});
});

describe('Safe Object', () => {
Expand Down Expand Up @@ -379,6 +395,8 @@ describe('Typy', () => {
const numberType = t(123);
expect(numberType.isNumber === true).toBeTruthy();
expect(stringType.isString === true).toBeTruthy();
const dateType = t(new Date());
expect(dateType.isDate === true).toBeTruthy();
});
});

Expand Down Expand Up @@ -410,7 +428,8 @@ describe('Typy', () => {
}
]
}
]
],
lastSeen: new Date()
};

const superheroSchema = {
Expand All @@ -425,7 +444,8 @@ describe('Typy', () => {
}
]
}
]
],
lastSeen: Schema.Date
};

test('should return true if object and schema are a valid match', () => {
Expand Down