-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
defb1e1
commit d90b391
Showing
29 changed files
with
277 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ src | |
config | ||
ts* | ||
*.md | ||
.eslintrc | ||
.eslintrc | ||
scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
(async function() { | ||
const fs = require('fs') | ||
const arr = ['table of contents'] | ||
let tableHeader = '|' | ||
let tableSeparator = '|' | ||
const takorContent = [] | ||
const read = new Promise(resolve => { | ||
fs.readdir('takor', (err, files) => { | ||
for (let file of files) { | ||
const text = fs.readFileSync(`./takor/${file}`, 'utf8') | ||
const name = text.split('\n')[0].replace(/[^a-zA-Z]/g, '') | ||
arr.push(name) | ||
tableHeader += (`${name} |`) | ||
tableSeparator += (`-|`) | ||
takorContent.push(text) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
|
||
await read | ||
const toc = [arr[0]].concat(arr.slice(1).map(el => `* [${el}](#${el})`)).join('\n') | ||
console.log(tableHeader) | ||
console.log(tableSeparator) | ||
})() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Flexible and composable runtime type assertion for Javascript. Syntax inspired by prop-types. Supports: | ||
- ES6 data structures | ||
- nested assertions | ||
- inverse assertions | ||
- custom validators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
(function () { | ||
var typeMatchers = require('../src/typeMatchers'); | ||
var keys = __spreadArrays(typeMatchers).map(function (_a) { | ||
var el = _a[0]; | ||
console.log(el.name || 'hhello'); | ||
}); | ||
console.log('????'); | ||
console.log(keys); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(function() { | ||
const typeMatchers = require('../src/typeMatchers') | ||
|
||
const keys = [...typeMatchers].map(([el]) => { | ||
console.log(el.name || 'hhello') | ||
}) | ||
console.log('????') | ||
console.log(keys) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
## 📦 Quick Examples | ||
```javascript | ||
// takor.oneOf | ||
const isNumOrStr = takor.oneOf(Number, String) | ||
isNumOrStr(10) // true | ||
isNumOrStr(new Set) // false | ||
|
||
// takor.shape basic | ||
const checkShape = takor.shape({ | ||
key1: takor.oneOf(Number, String) | ||
}) | ||
checkShape({ // true | ||
key1: 'string' | ||
}) | ||
|
||
// takor.shape with nested assertions | ||
const checkNestedElement = takor.shape({ | ||
key1: takor.shape({ | ||
key2: takor.arrayOf(Number, Set, String) | ||
}) | ||
}) | ||
checkNestedElement({ // true | ||
key1: { | ||
key2: [0, new Set, ''] | ||
} | ||
}) | ||
|
||
checkNestedElement({ // false | ||
key2: { | ||
key1: [0, new Set, ''] | ||
} | ||
}) | ||
|
||
// supports literal number or string | ||
const isValidDogBreed = takor.oneOf('terrier', 'greyhound', 'golden retriever') | ||
isValidDogBreed('terrier') // true | ||
isValidDogBreed('persian') // false | ||
|
||
// custom validator(s) | ||
const lessThanTen = (el) => el < 10 | ||
const greaterThanThree = (el) => el > 3 | ||
|
||
const goodNumberRange = takor.allOf(lessThanTen, greaterThanThree) | ||
const allInValidRange = takor.arrayOf(goodNumberRange, String) | ||
|
||
allInValidRange([8, 4, 3.5, 5]) // true | ||
allInValidRange([8, 4, '100', 5]) // true | ||
allInValidRange([8, 4, 100, 5]) // false | ||
allInValidRange(10) // false | ||
allInValidRange(new Map) // false | ||
|
||
// takor.mapOf | ||
const validMap = takor.mapOf( | ||
[Number, takor.oneOf(Array, Set)], | ||
[String, String] | ||
) | ||
validMap(new Map([ // true | ||
[10, []], | ||
[10, new Set], | ||
['10', '10'] | ||
])) | ||
|
||
validMap(new Map([ // false | ||
[10, []], | ||
[10, new Set], | ||
['10', new Set] | ||
])) | ||
|
||
// takor.not | ||
const nonNullOrArray = takor.not.oneOf(null, Array) | ||
nonNullOrArray(10) // true | ||
nonNullOrArray([]) // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## static methods | ||
|
||
| name | types | | ||
| -- | --| | ||
| shape | [el](#types) | ||
|
||
##types | ||
```typescript | ||
type IValidator = (el?: any) => boolean | ||
|
||
interface ShapeArg { | ||
[key: string]: IValidEnforcerArgs | ||
} | ||
|
||
type IValidEnforcerArgs = | ||
SetConstructor | | ||
MapConstructor | | ||
NumberConstructor | | ||
StringConstructor | | ||
null | | ||
undefined | | ||
ArrayConstructor | | ||
BooleanConstructor | | ||
true | | ||
false | | ||
string | | ||
number | | ||
IValidator | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#### allOf | ||
|
||
##### Desc | ||
Passed in validators must meet every criteria | ||
|
||
##### Notes | ||
- Contradictory validators will result in `false` | ||
|
||
##### Examples | ||
```javascript | ||
const isPopulated = (arr) => arr.length > 0 | ||
const populatedStringArr = Enforce.allOf(Enforce.arrayOf(String), isPopulated) | ||
populatedStringArr(['']) // true | ||
populatedStringArr([]) // false | ||
populatedStringArr([10]) // false | ||
|
||
// contradictory types. impossible to meet criteria | ||
const impossibleCheck = takor.allOf(Number, String) | ||
impossibleCheck(10) // false | ||
impossibleCheck('') // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#### shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## types | ||
|
||
```typescript | ||
export type IValidator = (el?: any) => boolean | ||
|
||
export type ShapeArg = { | ||
[key: string]: IValidEnforcerArgs | ||
} | ||
|
||
export type IValidEnforcerArgs = | ||
SetConstructor | | ||
MapConstructor | | ||
NumberConstructor | | ||
StringConstructor | | ||
null | | ||
undefined | | ||
ArrayConstructor | | ||
BooleanConstructor | | ||
true | | ||
false | | ||
string | | ||
number | | ||
IValidator | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
process.on('unhandledRejection', err => { | ||
throw err; | ||
}); | ||
|
||
const jest = require('jest'); | ||
let argv = process.argv.slice(2); | ||
|
||
// Watch unless on CI, in coverage mode, or explicitly running all tests | ||
if ( | ||
!process.env.CI && | ||
argv.indexOf('--coverage') === -1 && | ||
argv.indexOf('--watchAll') === -1 | ||
) { | ||
argv.push('--watch'); | ||
} | ||
|
||
|
||
jest.run(argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.