關於 is.js

關於 is.js

is.js 筆記

基礎四項判斷

is.
is.not
is.all
is.any

type checks 類型檢查

確認空值,is.null(null) //true
確認數值,is.number(42) //true
確認數值,is.number('42') //true
確認物件,is.object({foo:'bar'})  //true
確認陣列,is.Array(['foo', 'bar', 'baz']) //true
確認日期,is.data(new Date()) //true
確認字串,is.string('foo') //true
確認兩個類型是否相同,is.sameType(value:any, value:any),is.sameType(42, 7) //true
確認兩個類型是否相同,is.sameType(42, '7') //false

presence checks 存在檢查

確認空數值,is.empty({{}) //true
確認空數值,is.empty('') //true
確認有空格,is.space('   ') //true
確認信箱,is.email('test@test.com')  /true
確認字母英文,is.alphaNumeric('alphaNu3er1k')  //true
確認字母英文,is.alphaNumeric('*?') //false
--string checks 字符串檢查--
檢查B字串有在A字串中包含著,is.include('Some text goes here', 'text') //true
檢查B字串有在A字串中包含著,is.include('test', 'text') //false
檢查B字串有在A字串中包含著,is.not.include('test', 'text') //true
--arithmetic checks 算數檢查--
檢查B為等於A,is.equal(42, 40 + 2) //true
檢查B為等於A,is.equal('yeap', 'yeap') //true
檢查B不等於A,is.not.equal('yeap', 'nope') //true
檢查奇數,is.odd(41) //true
檢查數值為正數,is.positive(41) //true
檢查數值不為正數,is.not.positive(-42) //true
檢查數值為負數,is.negative(-41)  //true
檢查A數值大於B,is.above(41,30) //true
檢查A數值不大於B,is.not.above(42,50) //true
檢查A數值小於B,is,under(30,35) //true
檢查A數值不小於B,is.not.under(42,30) //true
檢查B數值在A、C之間,is.within(30, 20, 40) //true
檢查B數值不在A、C之間,is.not.within(40, 30, 35) //true
檢查數值為整數,is.integer(41) //true
檢查數值不為整數,is.not.integer(42.5) //true

object checks 物件檢查

檢查參數為當前物件長度,is.propertyCount({this: 'is', 'sample': object}, 2) //true

environment checks 環境檢查

檢查設備是否在線,is.online() //true if current device is online(如果當前設備在線,則為true)

time checks 時間檢查

檢查時間是今天,let today=new Date(); is.today(today); //true
檢查時間是昨天,let today = new Date(); is.yesterday(today); //false
檢查時間是明天,let today = new Date(); is.yesterday(today); //false
檢查時間是過去(以今天為主),
var yesterday = new Date(new Date().setDate(new Date().getDate() - 1));
var tomorrow = new Date(new Date().setDate(new Date().getDate() + 1));
is.past(yesterday); //true
is.past(tomorrow); //false
檢查時間是未來(以今天為主),
var yesterday = new Date(new Date().setDate(new Date().getDate() - 1));
var tomorrow = new Date(new Date().setDate(new Date().getDate() + 1));
is.future(yesterday); //false
is.future(tomorrow); //true
檢查時間是周末,
var monday = new Date('01/26/2015');
var sunday = new Date('01/25/2015');
var saturday = new Date('01/24/2015');
is.weekend(sunday) //true
is.weekend(monday); //false
檢查時間是工作日,
var monday = new Date('01/26/2015');
var sunday = new Date('01/25/2015');
var saturday = new Date('01/24/2015');
is.weekday(monday); //true
is.weekday(sunday); //false
檢查B時間在A、C時間之內,
var saturday = new Date('01/24/2015');
var sunday = new Date('01/25/2015');
var monday = new Date('01/26/2015');
is.inDateRange(sunday, saturday, monday); //true
檢查時間在前0~7天,
var twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2));
var nineDaysAgo = new Date(new Date().setDate(new Date().getDate() - 9));
is.inLastWeek(twoDaysAgo); //true
is.inLastWeek(nineDaysAgo); //false
檢查時間在前0~1個月,
var tenDaysAgo = new Date(new Date().setDate(new Date().getDate() - 10));
var fortyDaysAgo = new Date(new Date().setDate(new Date().getDate() - 40));
is.inLastMonth(tenDaysAgo); //true
is.inLastMonth(fortyDaysAgo); //false
檢查時間在前0~1年,
var twoMonthsAgo = new Date(new Date().setMonth(new Date().getMonth() - 2));
var thirteenMonthsAgo = new Date(new Date().setMonth(new Date().getMonth() - 13));
is.inLastYear(twoMonthsAgo); //true
檢查時間在後0~7天,
var twoDaysLater = new Date(new Date().setDate(new Date().getDate() + 2));
var nineDaysLater = new Date(new Date().setDate(new Date().getDate() + 9));
is.inNextWeek(twoDaysLater); //true
is.inNextWeek(nineDaysLater); //false
檢查時間在後0~1個月,
var tenDaysLater = new Date(new Date().setDate(new Date().getDate() + 10));
var fortyDaysLater = new Date(new Date().setDate(new Date().getDate() + 40));
is.inNextMonth(tenDaysLater); //true
is.inNextMonth(fortyDaysLater); //false
檢查時間在後0~1年,
var twoMonthsLater = new Date(new Date().setMonth(new Date().getMonth() + 2));
var thirteenMonthsLater = new Date(new Date().setMonth(new Date().getMonth() + 13));
is.inNextYear(twoMonthsLater); //true
is.inNextYear(thirteenMonthsLater); //false

參考資料

  1. 官網