Skip to content

Commit

Permalink
rearranged files
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew1007 committed Apr 5, 2020
1 parent defb1e1 commit d90b391
Show file tree
Hide file tree
Showing 29 changed files with 277 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ src
config
ts*
*.md
.eslintrc
.eslintrc
scripts
27 changes: 27 additions & 0 deletions docs/build.js
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)
})()

5 changes: 5 additions & 0 deletions docs/intro.md
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
16 changes: 16 additions & 0 deletions docs/makeAvailableValidators.js
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);
})();
9 changes: 9 additions & 0 deletions docs/makeAvailableValidators.ts
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)
})()
73 changes: 73 additions & 0 deletions docs/quick-examples.md
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
```
30 changes: 30 additions & 0 deletions docs/table.md
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

```
21 changes: 21 additions & 0 deletions docs/takor/allOf.md
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
```
1 change: 1 addition & 0 deletions docs/takor/shape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#### shape
24 changes: 24 additions & 0 deletions docs/types.md
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 added docs/validatorArgs.md
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "npm run build && nodemon dist/index.js",
"lint": "eslint src/*.ts",
"lint:fix": "eslint src/*.ts --fix",
"test": "node config/jest/test.js --modulePaths=src CI=true",
"test": "node scripts/test.js",
"ship": "npm run build && npm publish"
},
"repository": {
Expand Down
20 changes: 20 additions & 0 deletions scripts/test.js
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);
39 changes: 26 additions & 13 deletions src/__tests__/readmeExamples.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Enforce from '..'
import takor from '..'

describe('README examples', () => {
describe('Enforce.oneOf', () => {
const isNumOrStr = Enforce.oneOf(Number, String)
const isNumOrStr = takor.oneOf(Number, String)
it('matching value', () => {
expect(isNumOrStr(10)).toEqual(true)
})
Expand All @@ -12,8 +12,8 @@ describe('README examples', () => {
})

describe('Enforce.shape', () => {
const checkShape = Enforce.shape({
key1: Enforce.oneOf(Number, String)
const checkShape = takor.shape({
key1: takor.oneOf(Number, String)
})
describe('basic', () => {
it('matches', () => {
Expand All @@ -23,9 +23,9 @@ describe('README examples', () => {
})
})
describe('nested', () => {
const checkNestedElement = Enforce.shape({
key1: Enforce.shape({
key2: Enforce.arrayOf(Number, Set, String)
const checkNestedElement = takor.shape({
key1: takor.shape({
key2: takor.arrayOf(Number, Set, String)
})
})
it('matching value', () => {
Expand All @@ -46,7 +46,7 @@ describe('README examples', () => {
})

describe('literal number or string', () => {
const isValidDogBreed = Enforce.oneOf('terrier', 'greyhound', 'golden retriever')
const isValidDogBreed = takor.oneOf('terrier', 'greyhound', 'golden retriever')
it('matches', () => {
expect(isValidDogBreed('terrier')).toEqual(true)
})
Expand All @@ -59,8 +59,8 @@ describe('README examples', () => {
const lessThanTen = (el) => el < 10
const greaterThanThree = (el) => el > 3

const goodNumberRange = Enforce.allOf(lessThanTen, greaterThanThree)
const allInValidRange = Enforce.arrayOf(goodNumberRange, String)
const goodNumberRange = takor.allOf(lessThanTen, greaterThanThree)
const allInValidRange = takor.arrayOf(goodNumberRange, String)
it('matches', () => {
expect(allInValidRange([8, 4, 3.5, 5])).toEqual(true)
})
Expand All @@ -80,7 +80,7 @@ describe('README examples', () => {

describe('not', () => {
describe('oneOf', () => {
const nonNullOrArray = Enforce.not.oneOf(null, Array)
const nonNullOrArray = takor.not.oneOf(null, Array)
it('matches', () => {
expect(nonNullOrArray(10)).toEqual(true)
})
Expand All @@ -91,8 +91,8 @@ describe('README examples', () => {
})

describe('mapOf', () => {
const validMap = Enforce.mapOf(
[Number, Enforce.oneOf(Array, Set)],
const validMap = takor.mapOf(
[Number, takor.oneOf(Array, Set)],
[String, String]
)
it('matches', () => {
Expand All @@ -111,4 +111,17 @@ describe('README examples', () => {

})
})

describe('allOf', () => {
const isPopulated = (arr) => arr.length > 0
const populatedStringArr = takor.allOf(takor.arrayOf(String), isPopulated)
const impossibleCheck = takor.allOf(Number, String)
console.log(impossibleCheck(10)) // false
console.log(impossibleCheck('')) // false
it('it meets expected values', () => {
expect(populatedStringArr([''])).toEqual(true)
expect(populatedStringArr([])).toEqual(false)
expect(populatedStringArr([10])).toEqual(false)
})
})
})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d90b391

Please sign in to comment.