Skip to content

Commit

Permalink
Merge pull request #204 from HMPO/FOR-469-create-new-character-counte…
Browse files Browse the repository at this point in the history
…r-using-gds-component

FOR-469: Add support for gds word and character counter
  • Loading branch information
BecioProton authored Oct 28, 2024
2 parents fc40b48 + f3acefc commit 72791d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/validation/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ let validators = {
return validators.string(value) && (value === '' || value.length <= length);
},

maxwords(text, length) {
if (!validators.string(text)) return false;
const words = text.match(/\S+/g) || [];
return words.length <= length;
},

exactlength(value, length) {
return validators.string(value) && (value === '' || value.length === length);
},
Expand Down
31 changes: 31 additions & 0 deletions test/validation/spec.validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,37 @@ describe('validators', () => {

});

describe('maxwords', () => {

describe('invalid values', () => {
let inputs = [
[undefined, 1],
[100, 10],
['asdfasdfasdf', 0],
['asdf asdf asdf asdf asdf asdf asdf asdf', 3]
];
_.each(inputs, i => {
it(testName(i), () => {
validators.maxwords.apply(null, i).should.not.be.ok;
});
});
});

describe('valid values', () => {
let inputs = [
['', 0],
['asdfasdf asdf asdf asdf asdf', 10],
['123', 4]
];
_.each(inputs, i => {
it(testName(i), () => {
validators.maxwords.apply(null, i).should.be.ok;
});
});
});

});

describe('exactlength', () => {

describe('invalid values', () => {
Expand Down

0 comments on commit 72791d6

Please sign in to comment.