Skip to content

Commit 8f75ce5

Browse files
committed
[docs] Replace example/ -> examples/
1 parent b1a3b10 commit 8f75ce5

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

examples/api.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {StrictType as T} from 'typed-props'
2+
3+
let userType
4+
let postType
5+
6+
userType = T.exact({
7+
id: T.number,
8+
username: T.string,
9+
email: T.string,
10+
balance: T.number,
11+
// Posts has author which type is User
12+
posts: () => postType,
13+
})
14+
15+
postType = T.exact({
16+
title: T.string,
17+
starts: T.number,
18+
// Author has type User which has posts
19+
author: () => userType,
20+
})
21+
22+
function typeIs(type) {
23+
return ({$type}) => $type === type
24+
}
25+
26+
export default T.select(
27+
[typeIs('http://api.rumk.in/v1#user'), userType],
28+
[typeIs('http://api.rumk.in/v1#post'), postType],
29+
)

examples/uniq-items.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {Type, UniqRule} from 'typed-props'
2+
3+
class UniqItems extends UniqRule {
4+
static ruleName = 'uniqueItems'
5+
6+
static format(key = 'id') {
7+
return {key}
8+
}
9+
10+
static check(items, {key}) {
11+
const keys = new Set()
12+
const issues = []
13+
14+
items.forEach((item, i) => {
15+
const id = item[key]
16+
17+
if (! keys.has(id)) {
18+
const issue = {
19+
rule: this.ruleName,
20+
path: [i],
21+
details: {
22+
reason: 'duplicate',
23+
keyProp: key,
24+
key: id,
25+
},
26+
}
27+
28+
issues.push(issue)
29+
}
30+
31+
keys.set(id)
32+
})
33+
34+
return issues
35+
}
36+
}
37+
38+
class MyType extends Type {
39+
static uniqueItems(...args) {
40+
const checks = UniqItems.create([], UniqItems.format(...args))
41+
42+
return new this(checks)
43+
}
44+
45+
uniqueItems(...args) {
46+
const checks = UniqItems.create(this.getChecks(), UniqItems.format(...args))
47+
48+
return new this.constructor(checks)
49+
}
50+
}
51+
52+
const type = MyType.object
53+
.instanceOf(Array)
54+
.uniqueItems('id')
55+
56+
const value = [
57+
{id: 1},
58+
{id: 2},
59+
{id: 2}, // Duplicate
60+
]
61+
62+
const issues = Type.check(value, type)
63+
// issues array contain single issue for last element:
64+
// {path: [2], rule: 'uniqueItems', details: {reason: 'duplicate', keyProp: 'id', key: 2}}
65+
66+
console.log(issues)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"typescript": "^3.3.4000"
2828
},
2929
"directories": {
30-
"example": "example",
30+
"example": "examples",
3131
"test": "test"
3232
},
3333
"dependencies": {},

0 commit comments

Comments
 (0)