Skip to content

Commit 9a1c316

Browse files
committed
Format code with prettier
1 parent 0f7b872 commit 9a1c316

File tree

8 files changed

+77
-36
lines changed

8 files changed

+77
-36
lines changed

.prettierrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"arrowParens": "avoid",
3+
"bracketSpacing": true,
4+
"htmlWhitespaceSensitivity": "css",
5+
"insertPragma": false,
6+
"jsxBracketSameLine": false,
7+
"jsxSingleQuote": false,
8+
"printWidth": 80,
9+
"proseWrap": "always",
10+
"quoteProps": "as-needed",
11+
"requirePragma": false,
12+
"semi": false,
13+
"singleQuote": true,
14+
"tabWidth": 2,
15+
"trailingComma": "all",
16+
"useTabs": false
17+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const rules = {
77
fields: {
88
email: {
99
validator: () => {
10-
return email(value) && length(value, { max: 75 })
10+
return length(value, { max: 75 }) && email(value)
1111
},
12+
message: 'The given email address is invalid',
1213
},
1314
password: {
1415
validator: () => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"test:open": "open coverage/lcov-report/index.html"
1212
},
1313
"devDependencies": {
14-
"jest": "^26.2.2"
14+
"jest": "^26.2.2",
15+
"prettier": "^2.0.5"
1516
}
1617
}

src/__tests__/validate.test.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ it('initializes', () => {
66
const fields = {}
77
const rules = {}
88

9-
validate(fields, {
10-
rules,
11-
}, callback)
9+
validate(
10+
fields,
11+
{
12+
rules,
13+
},
14+
callback,
15+
)
1216
})
1317

1418
it('calls the correct validation checks and calls them with the current value', () => {
@@ -17,7 +21,7 @@ it('calls the correct validation checks and calls them with the current value',
1721

1822
const fields = {
1923
email,
20-
password
24+
password,
2125
}
2226

2327
const emailValidator = jest.fn(() => true)
@@ -26,12 +30,12 @@ it('calls the correct validation checks and calls them with the current value',
2630
const rules = {
2731
fields: {
2832
email: {
29-
validator: emailValidator
33+
validator: emailValidator,
3034
},
3135
password: {
32-
validator: passwordValidator
33-
}
34-
}
36+
validator: passwordValidator,
37+
},
38+
},
3539
}
3640

3741
const { valid } = validate(fields, {
@@ -53,17 +57,18 @@ it('shows a message when a field is entered but there are no rules for it', () =
5357
const { valid, results } = validate(fields, {
5458
rules: {
5559
fields: {},
56-
}
60+
},
5761
})
5862

59-
const expected = [
60-
{
61-
field: 'email',
62-
valid: false,
63-
message: 'No validation rules for field email',
64-
}
65-
]
66-
6763
expect(valid).toEqual(false)
68-
expect(results).toEqual(expected)
64+
expect(results).toMatchInlineSnapshot(`
65+
Array [
66+
Object {
67+
"field": "email",
68+
"message": "No validation rules for field 'email'",
69+
"valid": false,
70+
"value": "[email protected]",
71+
},
72+
]
73+
`)
6974
})

src/validate.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
function validate (fields, { rules }) {
1+
function validate(fields, { rules }) {
22
if (isEmpty(fields)) {
33
return { valid: false, results: [] }
44
}
55

66
const checked = []
77

8-
Object.keys(fields).forEach((field) => {
8+
Object.keys(fields).forEach(field => {
99
const rule = rules.fields[field]
1010

1111
if (!rule) {
12-
checked.push({ field, valid: false, message: `No validation rules for field ${field}`})
12+
checked.push({
13+
field,
14+
value: fields[field],
15+
valid: false,
16+
message: `No validation rules for field '${field}'`,
17+
})
1318
return
1419
}
1520

1621
if (rule.validator(fields[field])) {
17-
checked.push({ field, valid: true, message: '' })
22+
checked.push({ field, value: fields[field], valid: true, message: '' })
1823
} else {
19-
checked.push({ field, valid: false, message: rules.fields[field].message || 'Validation failed for field' })
24+
checked.push({
25+
field,
26+
value: fields[field],
27+
valid: false,
28+
message:
29+
rules.fields[field].message ||
30+
`Validation failed for field '${field}'`,
31+
})
2032
}
2133
})
2234

2335
return {
24-
valid: checked.every((current) => current.valid === true),
25-
results: checked
36+
valid: checked.every(current => current.valid === true),
37+
results: checked,
2638
}
2739
}
2840

2941
function isEmpty(obj) {
30-
return Object.keys(obj).length === 0;
42+
return Object.keys(obj).length === 0
3143
}
3244

3345
module.exports = {
3446
validate,
35-
...require('./validation/index.js')
47+
...require('./validation/index.js'),
3648
}

src/validation/__tests__/length.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const string = 'string'
44

55
it('checks for the length of a min value', () => {
66
const result = length(string, {
7-
min: 4
7+
min: 4,
88
})
99

1010
expect(result).toEqual(true)
1111
})
1212

1313
it('checks for the length of a max value', () => {
1414
const result = length(string, {
15-
max: 7
15+
max: 7,
1616
})
1717

1818
expect(result).toEqual(true)
@@ -37,7 +37,7 @@ it('returns false when the max length is not reached', () => {
3737
it('works with a min and a max value', () => {
3838
const result = length('strings', {
3939
min: 4,
40-
max: 5
40+
max: 5,
4141
})
4242

4343
expect(result).toEqual(false)

src/validation/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function number (x, { max, min } = {}) {
1+
function number(x, { max, min } = {}) {
22
if (min || max) {
33
if (Number(x) === x) {
44
if (x >= min && x <= max) return true
@@ -7,19 +7,19 @@ function number (x, { max, min } = {}) {
77
}
88
}
99

10-
if (Number(x) === x && (min === undefined && max === undefined)) {
10+
if (Number(x) === x && min === undefined && max === undefined) {
1111
return true
1212
}
1313

1414
return false
1515
}
1616

17-
function email (x) {
17+
function email(x) {
1818
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
1919
return emailRegex.test(x)
2020
}
2121

22-
function length (x, { min, max }) {
22+
function length(x, { min, max }) {
2323
const length = x.length
2424

2525
if (length >= min && length <= max) return true

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,11 @@ prelude-ls@~1.1.2:
26842684
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
26852685
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
26862686

2687+
prettier@^2.0.5:
2688+
version "2.0.5"
2689+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
2690+
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==
2691+
26872692
pretty-format@^26.2.0:
26882693
version "26.2.0"
26892694
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-26.2.0.tgz#83ecc8d7de676ff224225055e72bd64821cec4f1"

0 commit comments

Comments
 (0)