Skip to content

Instantly share code, notes, and snippets.

@ternavsky
Created July 25, 2024 12:31
Show Gist options
  • Save ternavsky/dca3594fa271366dddee7b0c00a397d5 to your computer and use it in GitHub Desktop.
Save ternavsky/dca3594fa271366dddee7b0c00a397d5 to your computer and use it in GitHub Desktop.
This tool lib allows to make strings checks and modification. It's useful for preparing requests for API calls to sanitize (str_mod) or validate (str_check) fields.
// Example usage
// Case 1
// const last6 = str_mod(trim(), last(6));
// console.log(last6(" test string ")); // returns "string"
// Case 2
// console.log(str_mod(" test string ", trim(), last(6))); // returns "string"
const trim = () => {
return (str) => str.trim();
};
const last = (n) => {
return (str) => str.slice(-n);
};
const first = (n) => {
return (str) => str.slice(0, n);
};
const defaultVal = (defaultValue) => {
return (str) => (str ? str : defaultValue);
};
const exclude = (regexp) => {
return (str) => str.replace(new RegExp(regexp, 'g'), '');
};
const alphanum = () => {
return (str) => str.replace(/[^a-zA-Z0-9]/g, '');
};
const num = () => {
return (str) => str.replace(/[^0-9]/g, '');
};
const inList = (arr) => {
return (str) => (arr.includes(str) ? str : null);
};
const min = (n) => {
return (str) => (str.length < n ? null : str);
};
const max = (n) => {
return (str) => (str.length > n ? null : str);
};
const length = (n) => {
return (str) => str.length === n ? str : null;
}
const join = (arr, separator = ', ') => {
return () => arr.filter(item => item).join(separator);
};
const firstNonEmpty = (arr) => {
return () => {
for (let item of arr) {
if (item) return item;
}
return null;
};
};
const getLastPart = (separator = ':') => {
return (str) => {
const parts = str.split(separator);
return parts[parts.length - 1];
};
};
const addTimeStamp = () => {
return (str) => `${str}_${Date.now()}`;
};
const toInt = () => {
return (str) => {
const match = str.match(/^(\d+)$/);
return match ? parseInt(match[0], 10) : null;
};
};
const toNumber = () => {
return (str) => {
const match = str.match(/^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/);
return match ? parseFloat(match[0]) : null;
};
};
// Define the str_mod function
const str_mod = (strOrFunc, ...funcs) => {
// trim string on start and end of processing
funcs.shift(trim());
funcs.push(trim());
const applyFuncs = (str, funcs) => {
return funcs.reduce((acc, func) => acc == null ? null : func(acc), str);
};
if (typeof strOrFunc === 'string') {
// If the first argument is a string, apply all funcs to the string
return applyFuncs(strOrFunc, funcs);
} else {
// Otherwise, return a function that takes a string and applies all funcs
return (str) => applyFuncs(str, [strOrFunc, ...funcs]);
}
};
const str_check = (strOrFunc, ...funcs) => {
if (typeof strOrFunc === 'string') {
// If the first argument is a string, apply all funcs to the string
const initialStr = strOrFunc.trim();
const finalStr = str_mod(strOrFunc, ...funcs);
return initialStr === finalStr ? finalStr : null;
} else {
// Otherwise, return a function that takes a string and applies all funcs
return (str) => {
// applyFuncs(str, [strOrFunc, ...funcs]);
const initialStr = str.trim();
const finalStr = str_mod(str, strOrFunc, ...funcs);
return initialStr === finalStr ? finalStr : null;
}
}
};
module.exports = {
str_mod,
str_check,
trim,
last,
first,
defaultVal,
exclude,
alphanum,
num,
inList,
min,
max,
length,
join,
firstNonEmpty,
getLastPart,
addTimeStamp,
toInt,
toNumber
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment