Skip to content

Instantly share code, notes, and snippets.

@kyrene-chew
kyrene-chew / SingaporeNricValidator.js
Last active August 11, 2023 06:17
Javascript - Singapore NRIC Validator
function nricValidator(nricInput) {
// validation rules
const nricRegex = /(\D)(\d{7})(\D)/;
const nricTypeRegex = /S|T|F|G/;
const weightArr = [2, 7, 6, 5, 4, 3, 2];
const nricLetterST = ['J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'];
const nricLetterFG = ['X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K'];
// set nric to all uppercase to remove case sensitivity
const nric = nricInput.toUpperCase();
@kyrene-chew
kyrene-chew / string.js
Last active May 9, 2017 08:36
JavaScript - String Handling
var string = {
// pad string with 0 from the left
padLeft: function (nr, n, str) { // pad string with 0 from the left
if (typeof nr === 'undefined' || nr === '') {
return '';
}
return Array(n - String(nr).length + 1).join(str || '0') + nr;
},
// camelcase
@kyrene-chew
kyrene-chew / htmlEntities.js
Last active May 9, 2017 08:36
JavaScript - Handles encoding and decoding of HTML entities
htmlEntities = {
// encode special characters to html encoded characters
encode: function (text) {
var encodedString = '';
for (i = 0; i < text.length; i++) {
if (text.charCodeAt(i) > 127) {
encodedString += '&#' + text.charCodeAt(i) + ';';
} else {
encodedString += text.charAt(i);
}
@kyrene-chew
kyrene-chew / cookie.js
Last active May 9, 2017 08:35
JavaScript - Cookie Handling
@kyrene-chew
kyrene-chew / getObject.ts
Last active May 9, 2017 08:37
TypeScript - Get object and their values with validation
/**
* Checks through entire datalayer object tree to see if object value exists
* sample usage: getObject(object, 'object.key1.key2.key3')
* @param {object} datalayer - datalayer object
* @param {string} objectKey? - optional
* @returns {string} returns value
*/
export function getObjectValue(datalayer: object, objectKey?: string): string {
const object = objectCheck(datalayer, objectKey);
if (typeof object === 'string') {