Skip to content

Commit 4a348aa

Browse files
committed
feat: use lab instead of tape
1 parent c39ddf9 commit 4a348aa

8 files changed

+2876
-567
lines changed

.eslintrc.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"extends": [
3+
"airbnb-base",
4+
"prettier"
5+
],
6+
"plugins": [
7+
"import",
8+
"prettier"
9+
],
10+
"rules": {
11+
"eqeqeq": ["warn"],
12+
"radix": ["warn"],
13+
"newline-per-chained-call": ["warn"],
14+
"no-param-reassign": ["error", { "props": false }],
15+
"import/no-dynamic-require": ["warn"],
16+
"prettier/prettier": ["error", {
17+
"singleQuote": true,
18+
"semi": false,
19+
"tabWidth": 4
20+
}]
21+
}
22+
}

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
3+
os:
4+
- linux
5+
- osx
6+
7+
node_js:
8+
- '6'
9+
- '7'
10+
- '8'
11+
12+
script:
13+
- npm test

lib/index.js

+45-62
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict'
2-
31
const Joi = require('joi')
42
const orderBy = require('lodash.orderby')
53
const find = require('lodash.find')
64
const map = require('lodash.map')
7-
const assert = require('assert')
5+
// const assert = require('assert')
86

97
module.exports = [
108
{
@@ -14,76 +12,61 @@ module.exports = [
1412
continuous_from: 'must be start from {{startIndex}}',
1513
continuous_broken: 'should be {{expectedValue}}'
1614
},
17-
coerce(value, state, options) {
18-
const { comparator, startIndex } = this._flags
19-
20-
assert.ok(
21-
comparator === undefined ||
22-
typeof comparator === 'function' ||
23-
typeof comparator === 'string',
24-
'comparator must be a function or a string'
25-
)
26-
27-
if (comparator) {
28-
const localState = {
29-
key: state.key,
30-
path: state.path.concat(comparator),
31-
parent: state.parent,
32-
reference: state.reference
33-
}
34-
35-
const records = orderBy(map(value, comparator))
36-
37-
if (records[0] !== startIndex) {
38-
const context = { comparator, records, startIndex }
15+
rules: [
16+
{
17+
name: 'continuous',
18+
description(params) {
19+
return `${params.comparator} must be an integer and started continuous from ${params.startIndex}`
20+
},
21+
params: {
22+
comparator: Joi.string().required(),
23+
startIndex: Joi.number()
24+
.integer()
25+
.default(0)
26+
},
27+
validate(params, value, state, options) {
28+
const { comparator, startIndex } = params
3929

40-
return this.createError(
41-
'array.continuous_from',
42-
context,
43-
localState,
44-
options
45-
)
46-
}
30+
const localState = {
31+
key: state.key,
32+
path: state.path.concat(comparator),
33+
parent: state.parent,
34+
reference: state.reference
35+
}
4736

48-
for (let idx = 1; idx < records.length; idx++) {
49-
const diff = records[idx] - records[idx - 1]
37+
const records = orderBy(map(value, comparator))
5038

51-
if (diff !== 1) {
52-
const obj = find(value, i => i.idx === records[idx])
53-
const context = {
54-
comparator,
55-
value: obj,
56-
expectedValue: records[idx - 1] + 1
57-
}
39+
if (records[0] !== startIndex) {
40+
const context = { comparator, records, startIndex }
5841

5942
return this.createError(
60-
'array.continuous_broken',
43+
'array.continuous_from',
6144
context,
6245
localState,
6346
options
6447
)
6548
}
66-
}
67-
}
6849

69-
return value
70-
},
71-
rules: [
72-
{
73-
name: 'continuous',
74-
description(params) {
75-
return `${params.comparator} must be an integer and started continuous from ${params.startIndex}`;
76-
},
77-
params: {
78-
comparator: Joi.any(),
79-
startIndex: Joi.number().integer().optional()
80-
},
81-
setup(params) {
82-
this._flags.comparator = params.comparator
83-
this._flags.startIndex = params.startIndex || 0
84-
},
85-
validate(params, value, state, options) {
86-
// No-op just to enable description
50+
for (let idx = 1; idx < records.length; idx += 1) {
51+
const diff = records[idx] - records[idx - 1]
52+
53+
if (diff !== 1) {
54+
const obj = find(value, i => i.idx === records[idx])
55+
const context = {
56+
comparator,
57+
value: obj,
58+
expectedValue: records[idx - 1] + 1
59+
}
60+
61+
return this.createError(
62+
'array.continuous_broken',
63+
context,
64+
localState,
65+
options
66+
)
67+
}
68+
}
69+
8770
return value
8871
}
8972
}

0 commit comments

Comments
 (0)