Skip to content

Commit d078bac

Browse files
authored
Merge pull request #1 from tuananh/feature/remove-lodash-and-hoek
dep: remove lodash and hoek, use core assert
2 parents 161284c + 418e0c7 commit d078bac

File tree

4 files changed

+96
-73
lines changed

4 files changed

+96
-73
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Joi extensions for extra array rules.
66
Usage is a two steps process. First, a schema is constructed using the provided types and constraints:
77

88
```js
9-
const BaseJoi = require('joi');
10-
const Extension = require('joi-date-extensions');
11-
const Joi = BaseJoi.extend(Extension);
9+
const BaseJoi = require('joi')
10+
const Extension = require('joi-array-extensions')
11+
const Joi = BaseJoi.extend(Extension)
1212

1313
const schema = Joi.array().items({
1414
idx: Joi.number().integer()
15-
}).continuous('idx', 1);
15+
}).continuous('idx', 1)
1616
```
1717

1818
# API

lib/index.js

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,89 @@
1-
'use strict';
1+
'use strict'
22

3-
const Joi = require('joi');
4-
const _ = require('lodash')
5-
const Hoek = require('hoek')
3+
const Joi = require('joi')
4+
const orderBy = require('lodash.orderby')
5+
const find = require('lodash.find')
6+
const map = require('lodash.map')
7+
const assert = require('assert')
68

7-
module.exports = [{
8-
name: 'array',
9-
base: Joi.array(),
10-
language: {
11-
continuous_from: 'must be start from {{startIndex}}',
12-
continuous_broken: 'should be {{expectedValue}}'
13-
},
14-
coerce(value, state, options) {
15-
const { comparator, startIndex } = this._flags
9+
module.exports = [
10+
{
11+
name: 'array',
12+
base: Joi.array(),
13+
language: {
14+
continuous_from: 'must be start from {{startIndex}}',
15+
continuous_broken: 'should be {{expectedValue}}'
16+
},
17+
coerce(value, state, options) {
18+
const { comparator, startIndex } = this._flags
1619

17-
Hoek.assert(comparator === undefined ||
18-
typeof comparator === 'function' ||
19-
typeof comparator === 'string', 'comparator must be a function or a string');
20+
assert.ok(
21+
comparator === undefined ||
22+
typeof comparator === 'function' ||
23+
typeof comparator === 'string',
24+
'comparator must be a function or a string'
25+
)
2026

21-
if (comparator) {
22-
const localState = {
23-
key: state.key,
24-
path: state.path.concat(comparator),
25-
parent: state.parent,
26-
reference: state.reference
27-
};
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+
}
2834

29-
const records = _.orderBy(_.map(value, comparator))
35+
const records = orderBy(map(value, comparator))
3036

31-
if (records[0] !== startIndex) {
32-
const context = { comparator, records, startIndex }
37+
if (records[0] !== startIndex) {
38+
const context = { comparator, records, startIndex }
3339

34-
return this.createError('array.continuous_from', context, localState, options);
35-
}
40+
return this.createError(
41+
'array.continuous_from',
42+
context,
43+
localState,
44+
options
45+
)
46+
}
3647

37-
for (let idx = 1; idx < records.length; idx++) {
38-
const diff = records[idx] - records[idx - 1];
48+
for (let idx = 1; idx < records.length; idx++) {
49+
const diff = records[idx] - records[idx - 1]
3950

40-
if (diff !== 1) {
41-
const obj = _.find(value, i => i.idx === records[idx])
42-
const context = {
43-
comparator,
44-
value: obj,
45-
expectedValue: records[idx - 1] + 1
46-
}
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+
}
4758

48-
return this.createError('array.continuous_broken', context, localState, options);
59+
return this.createError(
60+
'array.continuous_broken',
61+
context,
62+
localState,
63+
options
64+
)
65+
}
4966
}
5067
}
51-
}
5268

53-
return value;
54-
},
55-
rules: [
56-
{
57-
name: 'continuous',
58-
params: {
59-
comparator: Joi.any(),
60-
startIndex: Joi.number().integer().optional(),
61-
},
62-
setup(params) {
63-
this._flags.comparator = params.comparator;
64-
this._flags.startIndex = params.startIndex || 0;
65-
},
66-
validate(params, value, state, options) {
67-
// No-op just to enable description
68-
return value;
69+
return value
70+
},
71+
rules: [
72+
{
73+
name: 'continuous',
74+
params: {
75+
comparator: Joi.any(),
76+
startIndex: Joi.number().integer().optional()
77+
},
78+
setup(params) {
79+
this._flags.comparator = params.comparator
80+
this._flags.startIndex = params.startIndex || 0
81+
},
82+
validate(params, value, state, options) {
83+
// No-op just to enable description
84+
return value
85+
}
6986
}
70-
}
71-
]
72-
}];
87+
]
88+
}
89+
]

package-lock.json

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
},
2929
"homepage": "https://github.com/buianhthang/joi-array-extensions#readme",
3030
"dependencies": {
31-
"hoek": "^4.2.0",
32-
"lodash": "^4.17.4"
31+
"lodash.find": "^4.6.0",
32+
"lodash.map": "^4.6.0",
33+
"lodash.orderby": "^4.6.0"
3334
},
3435
"peerDependencies": {
3536
"joi": ">=10.x.x"

0 commit comments

Comments
 (0)