Skip to content

Commit 143301b

Browse files
committed
WIP same rule
1 parent 510a3c1 commit 143301b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/common/utils.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,30 @@ const isNumeric = (value: string): boolean => {
1212
return !isNaN(+value);
1313
}
1414

15+
const arraysEqual = (first: any[], second: any[]): boolean => {
16+
if (first.length !== second.length) {
17+
return false;
18+
}
19+
20+
let i = first.length;
21+
22+
while (i--) {
23+
const indexInSecond = second.findIndex((entry: any) => entry === first[i]);
24+
25+
if (indexInSecond !== -1) {
26+
second.splice(indexInSecond, 1);
27+
} else {
28+
return false;
29+
}
30+
31+
first.splice(i, 1);
32+
}
33+
34+
return first.length === 0 && second.length === 0;
35+
}
36+
1537
export {
1638
capitalize,
17-
isNumeric
39+
isNumeric,
40+
arraysEqual
1841
}

src/rules/same.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
getValue,
3+
} from '../common/dom';
4+
import { Validator } from '../Validator';
5+
import { arraysEqual } from '../common/utils';
6+
7+
export default(validator: Validator) => ({
8+
passed(elements: HTMLElement[], name: string): boolean {
9+
const values = elements.reduce((prev: string[], element: HTMLElement) => ([
10+
...prev,
11+
...getValue(element)
12+
]), []);
13+
14+
const otherValues = validator.refs(name).reduce((prev: string[], element: HTMLElement) => ([
15+
...prev,
16+
...getValue(element)
17+
]), []);
18+
19+
return arraysEqual(values, otherValues);
20+
},
21+
message(): string {
22+
return 'minLength';
23+
}
24+
});

0 commit comments

Comments
 (0)