Skip to content

Commit f2a0b75

Browse files
committed
[added] number methods less, more, notEqual
1 parent dab47a9 commit f2a0b75

File tree

3 files changed

+167
-74
lines changed

3 files changed

+167
-74
lines changed

src/locale.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export let string = {
2222
export let number = {
2323
min: '${path} must be greater than or equal to ${min}',
2424
max: '${path} must be less than or equal to ${max}',
25+
less: '${path} must be less than ${less}',
26+
more: '${path} must be greater than ${more}',
27+
notEqual: '${path} must be not equal to ${notEqual}',
2528
positive: '${path} must be a positive number',
2629
negative: '${path} must be a negative number',
2730
integer: '${path} must be an integer',

src/number.js

+111-74
Original file line numberDiff line numberDiff line change
@@ -9,86 +9,123 @@ let isNaN = value => value != +value
99
let isInteger = val => isAbsent(val) || val === (val | 0)
1010

1111
export default function NumberSchema() {
12-
if ( !(this instanceof NumberSchema))
13-
return new NumberSchema()
12+
if ( !(this instanceof NumberSchema))
13+
return new NumberSchema()
1414

15-
MixedSchema.call(this, { type: 'number' })
15+
MixedSchema.call(this, { type: 'number' })
1616

17-
this.withMutation(() => {
18-
this.transform(function(value) {
19-
if (this.isType(value)) return value
17+
this.withMutation(() => {
18+
this.transform(function(value) {
19+
if (this.isType(value)) return value
2020

21-
let parsed = parseFloat(value);
22-
if (this.isType(parsed)) return parsed
21+
let parsed = parseFloat(value);
22+
if (this.isType(parsed)) return parsed
2323

24-
return NaN;
25-
})
26-
})
24+
return NaN;
25+
})
26+
})
2727
}
2828

2929
inherits(NumberSchema, MixedSchema, {
3030

31-
_typeCheck(value) {
32-
if (value instanceof Number)
33-
value = value.valueOf();
34-
35-
return typeof value === 'number' && !isNaN(value)
36-
},
37-
38-
min(min, msg) {
39-
return this.test({
40-
name: 'min',
41-
exclusive: true,
42-
params: { min },
43-
message: msg || locale.min,
44-
test(value) {
45-
return isAbsent(value) || value >= this.resolve(min)
46-
}
47-
})
48-
},
49-
50-
max(max, msg) {
51-
return this.test({
52-
name: 'max',
53-
exclusive: true,
54-
params: { max },
55-
message: msg || locale.max,
56-
test(value) {
57-
return isAbsent(value) || value <= this.resolve(max)
58-
}
59-
})
60-
},
61-
62-
positive(msg) {
63-
return this.min(0, msg || locale.positive)
64-
},
65-
66-
negative(msg) {
67-
return this.max(0, msg || locale.negative)
68-
},
69-
70-
integer(msg) {
71-
msg = msg || locale.integer;
72-
73-
return this.test('integer', msg, isInteger)
74-
},
75-
76-
truncate() {
77-
return this.transform(value =>
78-
!isAbsent(value) ? (value | 0) : value)
79-
},
80-
81-
round(method) {
82-
var avail = ['ceil', 'floor', 'round', 'trunc']
83-
method = (method && method.toLowerCase()) || 'round'
84-
85-
// this exists for symemtry with the new Math.trunc
86-
if (method === 'trunc')
87-
return this.truncate()
88-
89-
if (avail.indexOf(method.toLowerCase()) === -1)
90-
throw new TypeError('Only valid options for round() are: ' + avail.join(', '))
91-
92-
return this.transform(value => !isAbsent(value) ? Math[method](value) : value)
93-
}
31+
_typeCheck(value) {
32+
if (value instanceof Number)
33+
value = value.valueOf();
34+
35+
return typeof value === 'number' && !isNaN(value)
36+
},
37+
38+
min(min, msg) {
39+
return this.test({
40+
name: 'min',
41+
exclusive: true,
42+
params: { min },
43+
message: msg || locale.min,
44+
test(value) {
45+
return isAbsent(value) || value >= this.resolve(min)
46+
}
47+
})
48+
},
49+
50+
max(max, msg) {
51+
return this.test({
52+
name: 'max',
53+
exclusive: true,
54+
params: { max },
55+
message: msg || locale.max,
56+
test(value) {
57+
return isAbsent(value) || value <= this.resolve(max)
58+
}
59+
})
60+
},
61+
62+
less(less, msg) {
63+
return this.test({
64+
name: 'less',
65+
exclusive: true,
66+
params: { less },
67+
message: msg || locale.less,
68+
test(value) {
69+
return isAbsent(value) || value < this.resolve(less)
70+
}
71+
})
72+
},
73+
74+
75+
more(more, msg) {
76+
return this.test({
77+
name: 'more',
78+
exclusive: true,
79+
params: { more },
80+
message: msg || locale.more,
81+
test(value) {
82+
return isAbsent(value) || value > this.resolve(more)
83+
}
84+
})
85+
},
86+
87+
notEqual(notEqual, msg) {
88+
return this.test({
89+
name: 'notEqual',
90+
exclusive: true,
91+
params: { notEqual },
92+
message: msg || locale.notEqual,
93+
test(value) {
94+
return isAbsent(value) || value !== this.resolve(notEqual)
95+
}
96+
})
97+
},
98+
99+
positive(msg) {
100+
return this.min(0, msg || locale.positive)
101+
},
102+
103+
negative(msg) {
104+
return this.max(0, msg || locale.negative)
105+
},
106+
107+
integer(msg) {
108+
msg = msg || locale.integer;
109+
110+
return this.test('integer', msg, isInteger)
111+
},
112+
113+
truncate() {
114+
return this.transform(value =>
115+
!isAbsent(value) ? (value | 0) : value)
116+
},
117+
118+
round(method) {
119+
var avail = ['ceil', 'floor', 'round', 'trunc']
120+
method = (method && method.toLowerCase()) || 'round'
121+
122+
// this exists for symemtry with the new Math.trunc
123+
if (method === 'trunc')
124+
return this.truncate()
125+
126+
if (avail.indexOf(method.toLowerCase()) === -1)
127+
throw new TypeError('Only valid options for round() are: ' + avail.join(', '))
128+
129+
return this.transform(value => !isAbsent(value) ? Math[method](value) : value)
130+
}
94131
})

test/number.js

+53
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,59 @@ describe('Number types', function() {
138138
})
139139
})
140140

141+
describe('less', () => {
142+
var schema = number().less(5);
143+
144+
TestHelpers.validateAll(schema, {
145+
valid: [
146+
4,
147+
-10,
148+
[null, schema.nullable()]
149+
],
150+
invalid: [
151+
5,
152+
7,
153+
null,
154+
[14, schema.less(10).less(14)]
155+
]
156+
})
157+
})
158+
159+
describe('more', () => {
160+
var schema = number().more(5);
161+
162+
TestHelpers.validateAll(schema, {
163+
valid: [
164+
6,
165+
56445435,
166+
[null, schema.nullable()]
167+
],
168+
invalid: [
169+
5,
170+
-10,
171+
null,
172+
[64, schema.more(52).more(74)]
173+
]
174+
})
175+
})
176+
177+
describe('notEqual', () => {
178+
var schema = number().notEqual(5);
179+
180+
TestHelpers.validateAll(schema, {
181+
valid: [
182+
6,
183+
56445435,
184+
[null, schema.nullable()]
185+
],
186+
invalid: [
187+
5,
188+
null,
189+
[52, schema.notEqual(52).notEqual(74)]
190+
]
191+
})
192+
})
193+
141194
describe('integer', ()=> {
142195
TestHelpers.validateAll(
143196
number().integer(),

0 commit comments

Comments
 (0)