JavaScript Array.every()
does not work the way I expected
I have been tasked with writing a basic check for GUID validity without using regex. So all I'm doing is checking if the non-dash characters in the guid string are hexa digits.
const isValidGUIDChar = (char) => !isNaN(+char) || "abcdef".includes(char.toLowerCase());
// let guid = "123";
// let guid = "abc"
let guid = "abc123";
let isGuidValid = guid.replaceAll('-', '').length === 32 && guid.split().every(isValidGUIDChar);
console.log(isGuidValid); // returns true for either 'abc' or '123' but false for 'abc123'
The results, mention in ment above are counter-intuitive. The every()
method seems to evaluate to:
"(every character must either be numeric), or (every character must be from among 'abcdef')",
whereas what I need, and what seems intuitive, would be for it to evaluate to:
"(every character) must (either be numeric from among 'abcdef')"
How can I acplish the latter?
JavaScript Array.every()
does not work the way I expected
I have been tasked with writing a basic check for GUID validity without using regex. So all I'm doing is checking if the non-dash characters in the guid string are hexa digits.
const isValidGUIDChar = (char) => !isNaN(+char) || "abcdef".includes(char.toLowerCase());
// let guid = "123";
// let guid = "abc"
let guid = "abc123";
let isGuidValid = guid.replaceAll('-', '').length === 32 && guid.split().every(isValidGUIDChar);
console.log(isGuidValid); // returns true for either 'abc' or '123' but false for 'abc123'
The results, mention in ment above are counter-intuitive. The every()
method seems to evaluate to:
"(every character must either be numeric), or (every character must be from among 'abcdef')",
whereas what I need, and what seems intuitive, would be for it to evaluate to:
"(every character) must (either be numeric from among 'abcdef')"
How can I acplish the latter?
Share edited Aug 18, 2021 at 19:23 Roko C. Buljan 207k41 gold badges328 silver badges340 bronze badges asked Aug 18, 2021 at 17:50 PakiPatPakiPat 1,0851 gold badge16 silver badges28 bronze badges 11-
Humm ... isn't
"0123456789abcdef"
shorter than!isNaN(+char) || "abcdef"
? Also see this:length === 32
– Roko C. Buljan Commented Aug 18, 2021 at 17:54 -
1
You've just forgotten to give
split
a separator, so you get a one-element array with the entire string in it, sochar
is the entire string. More: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… If you pass it""
(or, in modern code, use[...thestring]
), the check works. – T.J. Crowder Commented Aug 18, 2021 at 17:55 -
2
Also, instead of
guid.split("")
(which BTW is missing a""
) you could do just[...guid]
– Roko C. Buljan Commented Aug 18, 2021 at 17:58 -
2
@T.J.Crowder yes, but that is not a problem with
isValidGUIDChar
or even the.every
. Also, they should not be showing code that is an obvious problem that they've "skipped over" themselves. – crashmstr Commented Aug 18, 2021 at 18:00 - 1 @crashmstr - Yeah, bad phrasing on my part in my reply to you. I should have said "To see the problem the OP thinks they have..." :-) – T.J. Crowder Commented Aug 18, 2021 at 18:01
2 Answers
Reset to default 5- Your
.split()
is missing""
(BTW, use[...guid]
with Spread instead) - Make sure you actually test with 32 char after! you replace all dashes
- Use it as a function
isGuidValid(guid)
- reusability, remember?
const isHEX = (ch) => "0123456789abcdef".includes(ch.toLowerCase());
const isGuidValid = (guid) => {
guid = guid.replaceAll("-", ""); // Format it first!
return guid.length === 32 && [...guid].every(isHEX);
};
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fd")); // true
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fz")); // false
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2f")); // false
A more robust approach, taking in consideration the RFC4122 treating a GIUD/UUID as groups of 8-4-4-4-12 codepoint chars each would be:
const isHEX = h => !isNaN(parseInt(h, 16));
const RFC4122Len = [8, 4, 4, 4, 12];
const isGuidValid = (guid) => {
// Must have 36 Chars per codepoint (32 + 4 dashes)
const codePointLenght = [...guid].length;
if (codePointLenght !== 36) return false;
// Must have 5 groups
const groups = guid.split("-");
if (groups.length !== 5) return false;
// RFC defines 5 groups of 8-4-4-4-12 codepoints chars each
const lenMatch = groups.map(gr => [...gr].length).every((len, i) => len === RFC4122Len[i]);
if (!lenMatch) return false;
// Finally join groups (without the "-" separators and check for each char is HEX)
return [...(groups.join(""))].every(isHEX);
};
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fd")); // true
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2fz")); // false
console.log(isGuidValid("adec3dc3-7025-4905-b584-d9e64117f2f")); // false
There's more to make it fully RFC4122 pliant but you got the general idea.
To check if the characters are hex digits just use this 3 lines :
function isHex(h)
{
let a = parseInt(h, 16);
return a.toString(16) === h;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856562a4597442.html
评论列表(0条)