Skip to content

Commit dd247cc

Browse files
Merge pull request #4 from ARCANESCRIPTS/develop
Adding the ability to add messages to the collection
2 parents 20e9def + 85d6e11 commit dd247cc

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ getFormat(): string;
5959
setFormat(format:string): void;
6060
```
6161

62+
```js
63+
/**
64+
* Add a message to the collection.
65+
*
66+
* @param {string} key
67+
* @param {string} message
68+
*
69+
* @return void
70+
*/
71+
add(key: string, message: string): void;
72+
```
73+
6274
```js
6375
/**
6476
* Merge a new array of messages into the collection.

src/FormErrors.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ class FormErrors implements FormErrorsInterface {
5959

6060
// Main Method //
6161

62+
/**
63+
* Add a message to the collection.
64+
*
65+
* @param {string} key
66+
* @param {string} message
67+
*
68+
* @return void
69+
*/
70+
add(key: string, message: string): void {
71+
if (this.isUnique(key, message)) {
72+
if (this.messages[key])
73+
this.messages[key].push(message);
74+
else
75+
this.messages[key] = [message];
76+
}
77+
}
78+
6279
/**
6380
* Merge a new array of messages into the collection.
6481
*
@@ -233,7 +250,7 @@ class FormErrors implements FormErrorsInterface {
233250
*
234251
* @return {string[]}
235252
*/
236-
private transform(messages, format: string, key) {
253+
private transform(messages: string[], format: string, key: string) {
237254
format = this.checkFormat(format);
238255

239256
return _.transform(messages, function(result, message) {
@@ -266,6 +283,22 @@ class FormErrors implements FormErrorsInterface {
266283

267284
return transformed;
268285
}
286+
287+
/**
288+
* Determine if a key and message combination already exists.
289+
*
290+
* @param {string} key
291+
* @param {string} message
292+
*
293+
* @return boolean
294+
*/
295+
private isUnique(key: string, message: string): boolean {
296+
if (this.messages[key]) {
297+
return ! this.messages[key].includes(message);
298+
}
299+
300+
return true;
301+
}
269302
}
270303

271304
export default FormErrors;

src/FormErrorsInterface.ts

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ interface FormErrorsInterface {
3232

3333
// Main Methods //
3434

35+
/**
36+
* Add a message to the collection.
37+
*
38+
* @param {string} key
39+
* @param {string} message
40+
*
41+
* @return void
42+
*/
43+
add(key: string, message: string): void;
44+
3545
/**
3646
* Merge a new array of messages into the collection.
3747
*

test/FormErrors.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,60 @@ describe('#reset()', () => {
482482
expect(errors.isEmpty()).toBe(true);
483483
});
484484
});
485+
486+
describe('#add()', () => {
487+
it('Can add message to the collection', () => {
488+
let errors = new FormErrors;
489+
490+
expect(errors.count()).toEqual(0);
491+
492+
errors.add('name', 'The name is required.');
493+
494+
expect(errors.count()).toEqual(1);
495+
496+
errors.add('name', 'The name must be an alpha-numeric.');
497+
498+
expect(errors.count()).toEqual(1);
499+
500+
errors.add('email', 'The email is required.');
501+
502+
expect(errors.count()).toEqual(2);
503+
504+
expect(errors.getMessages()).toEqual({
505+
'email': [
506+
'The email is required.'
507+
],
508+
'name': [
509+
'The name is required.',
510+
'The name must be an alpha-numeric.'
511+
]
512+
});
513+
});
514+
515+
it('Can only add unique messages to the collection', () => {
516+
let errors = new FormErrors;
517+
518+
expect(errors.count()).toEqual(0);
519+
520+
errors.add('name', 'The name is required.');
521+
522+
expect(errors.count()).toEqual(1);
523+
524+
errors.add('name', 'The name must be an alpha-numeric.');
525+
526+
expect(errors.count()).toEqual(1);
527+
528+
errors.add('name', 'The name is required.');
529+
530+
expect(errors.count()).toEqual(1);
531+
532+
errors.add('name', 'The name must be an alpha-numeric.');
533+
534+
expect(errors.getMessages()).toEqual({
535+
'name': [
536+
'The name is required.',
537+
'The name must be an alpha-numeric.'
538+
]
539+
});
540+
});
541+
});

0 commit comments

Comments
 (0)