-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Exact Value/s Condition for String Length & Tests required with README.md Update #2019
Changes from 24 commits
ae711d6
f93cf70
cdd2192
27a40c8
cf729d1
be706c4
db5b0c7
2e48053
c194526
21ec415
59abfc7
e1bb810
7c2c0cf
3ee58c0
d3e6ca5
b850b84
acfe071
cdb06a5
70775b4
a71b7ff
dc42e7f
fb0def3
ec1aa91
73dc505
c7605bc
93ac65f
bc693dc
a90171f
57a5633
642e603
bb02daf
2a0a428
1bf1ee4
9fd65ed
fd90691
d09738c
11aa19f
e63e7a5
3ef2588
3b5cbd9
2897ef9
22bb2f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,41 @@ export default function isLength(str, options) { | |
assertString(str); | ||
let min; | ||
let max; | ||
let exact; | ||
let result; | ||
let isValid = false; | ||
bevatsal1122 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (typeof (options) === 'object') { | ||
min = options.min || 0; | ||
max = options.max; | ||
exact = options.exact; | ||
} else { // backwards compatibility: isLength(str, min [, max]) | ||
min = arguments[1] || 0; | ||
max = arguments[2]; | ||
} | ||
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || []; | ||
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || []; | ||
const len = str.length - presentationSequences.length - surrogatePairs.length; | ||
return len >= min && (typeof max === 'undefined' || len <= max); | ||
result = len >= min && (typeof max === 'undefined' || len <= max); | ||
if (result === false || typeof exact === 'undefined') return result; | ||
bevatsal1122 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Array.isArray(exact) === true) { | ||
for (let oneElem of exact) { | ||
if (len === oneElem) { | ||
isValid = true; | ||
break; | ||
} | ||
} | ||
} else if (typeof exact === 'number') { | ||
if (len === exact) { | ||
isValid = true; | ||
} | ||
} else if (typeof exact === 'object') { | ||
for (let key in exact) { | ||
if (len === exact[key]) { | ||
isValid = true; | ||
} | ||
} | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This else statement is not needed since you already define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yess!! Removed the else statement. |
||
isValid = false; | ||
} | ||
return isValid; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4629,6 +4629,12 @@ describe('Validators', () => { | |
valid: [''], | ||
invalid: ['a', 'ab'], | ||
}); | ||
test({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this new test since it does not test the new functionality There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yess!! Removed that test. |
||
validator: 'isLength', | ||
args: [2, 8], | ||
valid: ['Helloo', 'Laptop', 'ab'], | ||
invalid: ['', 'a'], | ||
}); | ||
}); | ||
|
||
it('should validate isLocale codes', () => { | ||
|
@@ -4698,12 +4704,54 @@ describe('Validators', () => { | |
valid: ['abc', 'de', 'a', ''], | ||
invalid: ['abcd'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ max: 6, exact: 5 }], | ||
valid: ['abcde', 'fffde', 'allkk'], | ||
invalid: ['abcd', 'abcdef', 'vfd', 'ff', '', 'k'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ max: 6, exact: { } }], | ||
valid: [], | ||
invalid: ['abcd', 'abcdef', 'vfd', 'ff', '', 'k'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ min: 2, max: 6, exact: 5 }], | ||
valid: ['abcde', 'fffde', 'allkk'], | ||
invalid: ['bsa', 'vfvd', 'ff', '', 'k'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ exact: 2 }], | ||
valid: ['fg', 'ff', 'po'], | ||
invalid: ['bsa', 'vfvd', '', 'k'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ max: 0 }], | ||
valid: [''], | ||
invalid: ['a', 'ab'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ min: 5, max: 10, exact: [6, 8, 9] }], | ||
valid: ['helloguy', 'shopping', 'validator', 'length'], | ||
invalid: ['abcde', 'abcdefg'], | ||
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ min: 5, max: 10, exact: { first: 6, second: 8, third: 9 } }], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if one of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||
valid: ['helloguy', 'shopping', 'validator', 'length'], | ||
invalid: ['abcde', 'abcdefg', 'abcdefghij'], | ||
}); | ||
test({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this test since it tests the same as the test of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or adjust this test to test the combination of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the later one with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wrong about this test, the difference is that this is using a string of '9' instead of the number 9. That makes this a different test to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I have included the combination of a |
||
validator: 'isLength', | ||
args: [{ exact: '9' }], | ||
valid: [], | ||
invalid: ['a', 'abcd', 'abcdefghijkl'], | ||
}); | ||
bevatsal1122 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test({ | ||
validator: 'isLength', | ||
valid: ['a', '', 'asds'], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add here that
exact
can be a single number, an array or an object of several numbers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!! Updated the
README.md
.