This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns a function which runs given checkFn against a value. | |
const createRule = (checkFn, errorMessage) => { | |
return async (value) => await Promise.resolve(checkFn(value)) ? null : errorMessage; | |
} | |
// Returns a function which accepts a value to validate and runs all passed rules. | |
const defineValidator = (...rules) => { | |
return async (value) => { | |
for (const rule of rules) { | |
const errorMessage = await rule(value); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function isUsernameAvailable(username) { | |
const response = await fetch(`/api/checkusername/${username}`); // returns { available: true | false } | |
return (await response.json()).available; | |
} | |
async function validateUsername(username, usernameUniquenessCheckFn) { | |
if (typeof username !== "string" || username.trim() === "") { | |
return { valid: false, reason: "Username is needed" }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function validateUsername(username) { | |
if (typeof username !== "string" || username.trim() === "") { | |
return { valid: false, reason: "Username is needed" }; | |
} | |
if (!username.match(/^\w+$/)) { | |
return { valid: false, reason: "Username must consist of characters and numbers" }; | |
} | |
const response = await fetch(`/api/checkusername/${username}`); // returns { available: true | false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function validateUsername(username) { | |
const errorNode = document.getElementById("error-text"); | |
// Check if username is a non-empty string | |
if (typeof username !== "string" || username.trim() === "") { | |
errorNode.innerText = "Username is required"; | |
return false; | |
} | |
// Check if username format is valid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupBy(arr, func){ | |
let grouped = Object.create(null); | |
arr.forEach((item) => { | |
const key = func(item); | |
if (!grouped[key]) { | |
grouped[key] = []; | |
} | |
grouped[key].push(item); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupBy(arr, func){ | |
return arr | |
.reduce((acc, item) => { | |
const key = func(item); | |
if (!acc[key]) { | |
acc[key] = []; | |
} | |
acc[key].push(item); | |
return acc; | |
}, Object.create(null)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const members = [["name", "apple"], ["type", "fruit"]]; | |
const obj = Object.create(null); | |
members.forEach(([key, value]) => obj[key] = value); | |
console.log(obj); // { name: "apple", type: "fruit" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const members = [["name", "apple"], ["type", "fruit"]]; | |
const food = members.reduce((obj, [key, value]) => { | |
obj[key] = value; | |
return obj; | |
}, Object.create(null)); | |
console.log(food); // { name: "apple", type: "fruit" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numbers = [1, 2, 3]; | |
let total = 0; | |
numbers.forEach(num => total += num); | |
console.log(total) // 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numbers = [1, 2, 3]; | |
const total = numbers.reduce((sum, num) => sum + num); | |
console.log(total); // 6 |
NewerOlder