Comparing version 0.5.1 to 0.6.0
@@ -5,8 +5,10 @@ var isString = arg => typeof arg === 'string' | ||
var isStringAndNotEmpty = arg => isString(arg) && isNotEmptyString(arg) | ||
var isEmptyString = arg => isString(arg) && !arg | ||
var isEmptyString = arg => isString(arg) && !arg.trim() | ||
var isNotEmptyString = arg => !isEmptyString(arg) | ||
var isName = arg => isString(arg) && !!arg.trim() | ||
var isNotName = arg => !isName(arg) | ||
var isEmail = arg => isString(arg) && /\S+@\S+\.\S+/.test(arg) | ||
@@ -16,3 +18,3 @@ | ||
var isVariableName = (arg) => !!arg && new RegExp('^[a-zA-Z_$][0-9a-zA-Z_$]*$').test(arg) | ||
var isVariableName = (arg) => !!arg && new RegExp('^[a-zA-Z_$][0-9a-zA-Z_$]*$').test(arg) | ||
@@ -33,8 +35,17 @@ var isNotVariableName = (arg) => !isVariableName(arg) | ||
var isISOString = (arg) => /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[+-]\d{2}:\d{2})$/.test(arg) | ||
var isNotISOString = (arg) => !isISOString(arg) | ||
var isUUID = (arg) => /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(arg) | ||
var isNotUUID = (arg) => !isUUID(arg) | ||
module.exports = { | ||
isString: isString, | ||
isNotString:isNotString, | ||
isStringAndNotEmpty: isStringAndNotEmpty, | ||
isEmptyString: isEmptyString, | ||
isNotEmptyString: isNotEmptyString, | ||
isName: isName, | ||
isNotName: isNotName, | ||
isEmail: isEmail, | ||
@@ -45,3 +56,7 @@ isNotEmail: isNotEmail, | ||
isURL: isURL, | ||
isNotURL: isNotURL | ||
isNotURL: isNotURL, | ||
isISOString: isISOString, | ||
isNotISOString: isNotISOString, | ||
isUUID: isUUID, | ||
isNotUUID: isNotUUID | ||
} |
{ | ||
"name": "isnot", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "All imaginable type checking utils with negation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
185
README.md
# isnot | ||
Type checking package for JavaScript, include just once in your entrypoint like | ||
Beautifully documented type checking package for JavaScript. | ||
you need to fetch every single function like const {isString} = require('isnot'); | ||
## Usage | ||
To use the functions just do: | ||
## Available functions | ||
` var {isString, isArray} = require('isnot'); ` | ||
### Strings | ||
## TOC | ||
* [Strings](#Strings) | ||
* [String](#String) | ||
* [isString](#isString) | ||
* [isNotString](#isNotString) | ||
* [Empty String](#EmptyString) | ||
* [isEmptyString](#isEmptyString) | ||
* [isNotEmptyString](#isNotEmptyString) | ||
* [Name](#Name) | ||
* [isName](#isName) | ||
* [isNotName](#isNotName) | ||
* [Email](#Email) | ||
* [isEmail](#isEmail) | ||
* [isNotEmail](#isNotEmail) | ||
* [Variable Name](#VariableName) | ||
* [isVariableName](#isVariableName) | ||
* [isNotVariableName](#isNotVariableName) | ||
* [URL](#URL) | ||
* [isURL](#isURL) | ||
* [isNotURL](#isNotURL) | ||
* [ISO Date](#ISODate) | ||
* [isISODate](#isISODate) | ||
* [isNotISODate](#isNotISODate) | ||
* [UUID](#UUID) | ||
* [isUUID](#isUUID) | ||
* [isNotUUID](#isNotUUID) | ||
* [Arrays](#Arrays) | ||
* [Array](#Array) | ||
### Strings <a name="Strings"></a> | ||
#### String <a name="String"></a> | ||
Corresponds to the js string primitive | ||
###### isString <a name="isString"></a> | ||
~~~js | ||
isString('') //true | ||
~~~ | ||
###### isNotString <a name="isNotString"></a> | ||
~~~js | ||
isNotString(()=>{}) //true | ||
~~~ | ||
isStringAndNotEmpty('') //false | ||
#### Empty String <a name="EmptyString"></a> | ||
isStringAndNotEmpty(' ') //false | ||
An empty string is only `''` | ||
isStringAndNotEmpty('hey') //true | ||
###### isEmptyString <a name="isEmptyString"></a> | ||
Purely checks if the string is empty | ||
~~~js | ||
isEmptyString({}) //false | ||
@@ -27,8 +71,60 @@ | ||
isEmptyString(' ') //false | ||
~~~ | ||
###### isEmptyString <a name="isEmptyString"></a> | ||
~~~js | ||
isNotEmptyString('') //false | ||
~~~ | ||
#### Name <a name="Name"></a> | ||
A name is a string that is just not empty space. | ||
###### isName <a name="isName"></a> | ||
~~~js | ||
isName('') //false | ||
isName(' ') //false | ||
isName('\r') //false | ||
isName(123) //false | ||
isName('hey') //true | ||
~~~ | ||
###### isNotName <a name="isNotName"></a> | ||
~~~js | ||
isNotName('') //true | ||
isNotName(' ') //true | ||
isNotName('\n') //true | ||
isNotName([]) //true | ||
isNotName({}.toString()) //false | ||
~~~ | ||
#### Email <a name="Email"></a> | ||
###### isEmail <a name="isEmail"></a> | ||
~~~js | ||
isEmail('a@b.c') //true | ||
~~~ | ||
isNotEmail('whatever@domain') //true | ||
###### isNotEmail <a name="isNotEmail"></a> | ||
~~~js | ||
isNotEmail('missing@dot') //true | ||
isNotEmail('missing.domain') //true | ||
~~~ | ||
#### Variable Name <a name="VariableName"></a> | ||
###### isVariableName <a name="isVariableName"></a> | ||
~~~js | ||
isVariableName('var') //true (technically not, but it can be a property of an object accessed without ['...']) | ||
@@ -41,10 +137,81 @@ | ||
isVariableName('var0') //true | ||
~~~ | ||
###### isNotVariableName <a name="isNotVariableName"></a> | ||
~~~js | ||
isNotVariableName('a@a') //true | ||
isNotVariableName('--var--') //true | ||
~~~ | ||
### Arrays | ||
#### URL <a name="URL"></a> | ||
###### isURL <a name="isURL"></a> | ||
~~~js | ||
isURL('https://localhost') //true | ||
isURL('www.site.com') //true | ||
~~~ | ||
###### isNotURL <a name="isNotURL"></a> | ||
~~~js | ||
isNotURL('a@a') //true | ||
isNotURL('site.com') //false | ||
~~~ | ||
#### ISO String <a name="ISOString"></a> | ||
Checks only if string it's in a ISO format, not if the date is actually a valid date. | ||
Useful to check date properties stored in a database. | ||
###### isISOString <a name="isISOString"></a> | ||
~~~js | ||
isISOString(new Date().toISOString()) //true | ||
isISOString('2017-06-01T18:43:26.000-02:00') //true | ||
isISOString('2017-06-01T18:43:26.000Z') //true | ||
~~~ | ||
###### isNotISOString <a name="isNotISOString"></a> | ||
~~~js | ||
isNotISOString('2017-06-01') //true | ||
isNotISOString('2017/06/01') //true | ||
isNotISOString('2017/06/01T18:43:26.000-02:00') //true | ||
isNotISOString(new Date().toString()) //true | ||
~~~ | ||
#### UUID <a name="UUID"></a> | ||
Tests any version of UUID from 1 to 5. | ||
###### isUUID <a name="isUUID"></a> | ||
~~~js | ||
isUUID(uuidv1()) //true | ||
isUUID(uuidv2()) //true | ||
isUUID(uuidv3()) //true | ||
isUUID(uuidv4()) //true | ||
isUUID(uuidv5()) //true | ||
isUUID('5a2de30a-a736-5aea-8f7f-ad0f019cdc00') //true | ||
~~~ | ||
###### isNotUUID <a name="isNotUUID"></a> | ||
~~~js | ||
isNotUUID('2017-06-01') //true | ||
~~~ | ||
### Arrays <a name="Arrays"></a> | ||
#### Array <a name="Array"></a> | ||
~~~js | ||
isArray([]) // true | ||
@@ -51,0 +218,0 @@ |
@@ -5,5 +5,6 @@ var expect = require("chai").expect; | ||
isNotString, | ||
isStringAndNotEmpty, | ||
isEmptyString, | ||
isNotEmptyString, | ||
isEmptyString, | ||
isNotEmptyString, | ||
isName, | ||
isNotName, | ||
isEmail, | ||
@@ -14,3 +15,7 @@ isNotEmail, | ||
isURL, | ||
isNotURL | ||
isNotURL, | ||
isISOString, | ||
isNotISOString, | ||
isUUID, | ||
isNotUUID | ||
} = require("../lib/strings"); | ||
@@ -38,2 +43,8 @@ | ||
}); | ||
describe("isNotString", function() { | ||
it("checks correctly", function() { | ||
expect(isNotString('string')).to.equal(false); | ||
expect(isNotString(['string'])).to.equal(true); | ||
}); | ||
}); | ||
describe("isEmptyString", function() { | ||
@@ -43,6 +54,7 @@ it("checks correctly", function() { | ||
expect(isEmptyString('')).to.equal(true); | ||
expect(isEmptyString(' ')).to.equal(true); | ||
expect(isEmptyString(' ')).to.equal(false); | ||
expect(isEmptyString('\n')).to.equal(false); | ||
expect(isEmptyString(' - ')).to.equal(false); | ||
expect(isEmptyString('\t\t')).to.equal(true); | ||
expect(isEmptyString('\n\r')).to.equal(true); | ||
expect(isEmptyString('\t\t')).to.equal(false); | ||
expect(isEmptyString('\n\r')).to.equal(false); | ||
expect(isEmptyString(null)).to.equal(false); | ||
@@ -56,12 +68,33 @@ expect(isEmptyString(undefined)).to.equal(false); | ||
}); | ||
describe("isStringAndNotEmpty", function() { | ||
describe("isNotEmptyString", function() { | ||
it("checks correctly", function() { | ||
expect(isStringAndNotEmpty('')).to.equal(false); | ||
expect(isStringAndNotEmpty(' ')).to.equal(false); | ||
expect(isStringAndNotEmpty('a')).to.equal(true); | ||
expect(isStringAndNotEmpty([])).to.equal(false); | ||
expect(isStringAndNotEmpty({})).to.equal(false); | ||
expect(isStringAndNotEmpty(11)).to.equal(false); | ||
expect(isNotEmptyString('string')).to.equal(true); | ||
expect(isNotEmptyString('')).to.equal(false); | ||
}); | ||
}); | ||
describe("isName", function() { | ||
it("checks correctly", function() { | ||
expect(isName('')).to.equal(false); | ||
expect(isName(' ')).to.equal(false); | ||
expect(isName('\n')).to.equal(false); | ||
expect(isName('\r')).to.equal(false); | ||
expect(isName('\n\r')).to.equal(false); | ||
expect(isName('\t')).to.equal(false); | ||
expect(isName('a\t')).to.equal(true); | ||
expect(isName([])).to.equal(false); | ||
expect(isName({})).to.equal(false); | ||
expect(isName(11)).to.equal(false); | ||
}); | ||
}); | ||
describe("isNotName", function() { | ||
it("checks correctly", function() { | ||
expect(isNotName('')).to.equal(true); | ||
expect(isNotName(' ')).to.equal(true); | ||
expect(isNotName('a')).to.equal(false); | ||
expect(isNotName([])).to.equal(true); | ||
expect(isNotName({})).to.equal(true); | ||
expect(isNotName({}.toString())).to.equal(false); | ||
expect(isNotName(11)).to.equal(true); | ||
}); | ||
}); | ||
describe("isEmail", function() { | ||
@@ -86,2 +119,7 @@ it("checks correctly", function() { | ||
}); | ||
describe("isNotEmail", function() { | ||
it("checks correctly", function() { | ||
expect(isNotEmail('a@a.a')).to.equal(false); | ||
}); | ||
}); | ||
describe("isVariableName", function() { | ||
@@ -104,2 +142,7 @@ it("checks correctly", function() { | ||
}); | ||
describe("isNotVariableName", function() { | ||
it("checks correctly", function() { | ||
expect(isNotVariableName('a@a.a')).to.equal(true); | ||
}); | ||
}); | ||
@@ -120,2 +163,33 @@ describe("isURL", function() { | ||
}); | ||
describe("isNotURL", function() { | ||
it("checks correctly", function() { | ||
expect(isNotURL('a@a.a')).to.equal(true); | ||
}); | ||
}); | ||
describe("isISOString", function() { | ||
it("checks correctly", function() { | ||
expect(isISOString(new Date().toISOString())).to.equal(true); | ||
expect(isISOString('2017-06-01T18:43:26.000Z')).to.equal(true); | ||
expect(isISOString('2017-06-01T18:43:26.000-02:00')).to.equal(true); | ||
}); | ||
}); | ||
describe("isNotISOString", function() { | ||
it("checks correctly", function() { | ||
expect(isNotISOString(undefined)).to.equal(true); | ||
expect(isNotISOString('a@a.a')).to.equal(true); | ||
expect(isNotISOString(new Date().toString())).to.equal(true); | ||
}); | ||
}); | ||
describe("isUUID", function() { | ||
it("checks correctly", function() { | ||
expect(isUUID('416ac246-e7ac-49ff-93b4-f7e94d997e6b')).to.equal(true); | ||
}); | ||
}); | ||
describe("isNotUUID", function() { | ||
it("checks correctly", function() { | ||
expect(isNotUUID(undefined)).to.equal(true); | ||
expect(isNotUUID('1234-1234-1234')).to.equal(true); | ||
}); | ||
}); | ||
}); |
30290
662
327