UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

45 lines (43 loc) 2.19 kB
/** * * get array of strings only (aka words) from the item proprty Original code: const keyVals: string[] = item[key].replace(/[0-9]/g, " ").split(/\b([a-z]+)\b/gi); * https://github.com/mikezimm/Compliance/issues/138 * Test code: resulting in ["test","i","ng","as","dfasd","asdf"] const item = 'test1234~321i~ng as?!_$dfasd. -++[asdf];?~!&^$%...,;' const keyVals = item.replace(/[0-9.,;:?~!&^$%+_[\]-]/g, " ").replace(/\s\s+/g, ' ').split(/\b([a-z]+)\b/gi).filter( v => v.trim() !== '' ); console.log( keyVals ) * getStringArrayBasic is a simpler variation of getArrayOfWordsFromString * This will use exact case and not filter duplicates . * Finally it does not do any sorting. * * You can use getArrayOfWordsFromString IF YOU NEED any of those advanced options. * * @param baseStr * @param removeDigits * @returns */ export function getStringArrayBasic(baseStr, removeDigits) { if (!baseStr) return []; if (removeDigits === true) baseStr = baseStr.replace(/[0-9]/g, ' '); baseStr = baseStr.replace(/[.,;:?~!&^$%+_[\]-]+/g, ' '); // Replace most common special characters baseStr = baseStr.replace(/[()]+/g, ' '); // Replace most common special characters baseStr = baseStr.replace(/[/\\]+/g, ' '); // Replace most common special characters baseStr = baseStr.replace(/[']+/g, ' '); // Replace most common special characters baseStr = baseStr.replace(/\s\s+/g, ' '); // Replace multiple spaces with just one // https://github.com/mikezimm/Compliance/issues/141 // This was splitting words but also spliting words by international characters like Ü // const originalWords = baseStr.split(/\b([a-z]+)\b/gi) // get array of original trimmed words and exclude empties const originalWords = baseStr.split(' ') .map(word => { return word.trim(); }).filter(word => { return word; }); // NOTE: getSuggestionsByKeys used this code which may also be valid... seemed to work in testing // .filter( ( v: string ) => v.trim() !== '' ); // then remove spaces and empties return originalWords; } //# sourceMappingURL=getStringArrayBasic.js.map