Skip to content

Commit

Permalink
对异常值'',null,undefined进行判断
Browse files Browse the repository at this point in the history
  • Loading branch information
shanchao.wsc committed Dec 8, 2017
1 parent 94511dc commit face752
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.8
* `CHANGED` undefined/null/'' return ''
* because they are all invalid value for date.

# 0.1.7
* `CHANGED` null check

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uxcore-formatter",
"version": "0.1.7",
"version": "0.1.8",
"description": "a formatter library for uxcore",
"repository": "https://github.com/uxcore/uxcore-formatter.git",
"author": "eternalsky",
Expand Down
5 changes: 3 additions & 2 deletions src/Formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
const Formatter = {};

Formatter.date = (str, pattern) => {
// new Date(null) 会返回最初日期。
if(str == null){
// new Date(null,false) 会返回最初日期。
if (str === null || str === false || str === undefined || str === '') {
console.warn('Formatter: invalid date');
return '';
}
const date = new Date(str);
Expand Down
3 changes: 3 additions & 0 deletions tests/Formatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('Formatter.date', () => {

it('returns empty string when date is null', () => {
expect(Formatter.date(null, 'YYYY-MM-DD')).to.be('');
expect(Formatter.date(undefined, 'YYYY-MM-DD')).to.be('');
expect(Formatter.date(false, 'YYYY-MM-DD')).to.be('');
expect(Formatter.date('', 'YYYY-MM-DD')).to.be('');
});

it('works fine', () => {
Expand Down

0 comments on commit face752

Please sign in to comment.