loose-string-test v1.0.1
loose-string-test
Can test a string against a pattern for loose equality - ignoring unimportant whitespaces.
// looseStringTest(pattern: string, input: string)
looseStringTest('[a,b, c ]', '[a, b,c]'); // trueAlso does 'begins-with' tests:
// looseStringTest(pattern: string, input: string)
looseStringTest('[a,b,c ...', '[a, b, c, d, e, f]'); // true
looseStringTest('[a,b,d ...', '[a, b, c, d, e, f]'); // false...by having a three dots (...) appended to the end of its pattern argument value
Note: a pattern is a simple string expression, much simpler than a RegExp. More about patterns in the On Patterns chapter.
Should we need an exact comparison, enclose the pattern value in quotes:
// looseStringTest(pattern: string, input: string)
looseStringTest('"0042"', ' 0042'); // false
looseStringTest('" 0042"', ' 0042'); // true
looseStringTest("' 0042'", ' 0042'); // trueWhitespaces are significant in "strings in a string" loose comparison:
looseStringTest('["a b","cd" ...', '["a b", "cd", " ef"]'); // true
looseStringTest('["a b","cd" ...', '["ab", "cd", " ef"]'); // false
looseStringTest('{"name":"circle 1"...', '{ "name": "circle 1", "radius": 5 }'); // trueLoosely tests multi-lined strings:
const creature = {
name: 'R. Quack',
species: 'duck',
};
const creatureJSON = JSON.stringify(creature, null, ' ');
console.log(creatureJSON);
//=> {
// "name": "R. Quack",
// "species": "duck"
// }
console.log(looseStringTest('{"name":"R. Quack" ...', creatureJSON));
//=> trueInstallation
$ npm i loose-string-testUsage
Typescript / ES module:
import {looseStringTest} from 'loose-string-test';Javascript / CommonJS:
const {looseStringTest} = require('loose-string-test');About Lib
- Typed
- With
d.tsfor javascript - No dependencies
- Well tested, 100% code coverage
On Patterns
Patterns are strings, with special meaning to a loose-string-test library. The purpose of pattern is to be matched against an input string.
Patterns can be exact or loose, whole or start.
- exact pattern matches the exact the same input
- loose pattern matches the input, even if there are some whitespace difference
- whole pattern matches the input as a whole
- start pattern matches only the beginning of the input
Exact whole pattern
By enclosing whole pattern string value into quotes (either single or double ones), we'll make that string an exact whole pattern:
// looseStringTest(pattern: string, input: string)
looseStringTest('"abc d"', 'abcd'); //=> false
looseStringTest('"abc d"', 'abc d'); //=> trueWith the exact whole pattern argument, the looseStringTest functions just compares the body of that pattern with the input string for a string equality.
const exactWholePattern = '"search text"';
const exactWholePatternBody = 'search text';
// following function call:
looseStringTest(exactWholePattern, 'search text ');
// ... gives the same result as the expression:
exactWholePatternBody === 'search text ';
// ... i.e. false, because of trailing space at the end of the input string ('search text ')Exact Start Pattern
By adding three dots (...) after the quoted part of the pattern string, we'll make that pattern an exact start pattern:
// looseStringTest(pattern: string, input: string)
looseStringTest('"abc" ...', 'abcd'); //=> true
looseStringTest('"abc" ...', 'ab'); //=> falseWith the exact start pattern argument, the looseStringTest functions just perform the startsWith method:
const exactStartPattern = '"search text" ...';
const exactStartPatternBody = 'search text';
// following function call:
looseStringTest(exactStartPattern, 'search text ');
// ... gives the same result as the expression:
'search text '.startsWith(exactStartPatternBody);
// ... i.e. trueLoose Whole Pattern
loose whole pattern ignores spaces and EOLs:
// looseStringTest(pattern: string, input: string)
looseStringTest('abcd', 'abcd'); //=> true
looseStringTest(' a bc d ', ' ab cd'); //=> trueIt strips all the unimportant space and EOL characters from both the pattern and input before comparing them:
const looseWholePattern = ' search text ';
// following function call:
looseStringTest(looseWholePattern, 'search text ');
// ... gives the same result as the expression:
stripUnimportantWhitechars(looseWholePattern) ===
stripUnimportantWhitechars('search text ');
// ... trueThe only important whitespaces are ones inside quote pairs in the loose pattern:
looseStringTest(' [ "a", " b " ] ', '["a"," b "]'); // trueLoose Start Pattern
Every loose pattern that ends with three dots (...) is considered to be a loose Start pattern:
const looseStartPattern = 'abc ...';It strips all the unimportant space and EOL characters from both the pattern and input before comparing them:
const looseStartPattern = ' sea ...';
// following function call:
looseStringTest(looseStartPattern, 'search text ');
// ... gives the same result as the expression:
stripUnimportantWhitechars('search text ').startsWith(
stripUnimportantWhitechars(looseStartPattern)
);
// ... trueThe only important whitespaces are ones inside quote pairs in the loose start pattern:
looseStringTest(' [ "a", " b ", .... ', '["a"," b ","c "]'); // trueparsePattern Function
Gives you information about a pattern:
parsePattern('"abc"');
// {
// body: 'abc',
// stripped: 'abc',
// isExactPattern: true,
// isStartPattern: false
// }
parsePattern('["a", "b c", "d" ...');
// {
// body: '["a", "b c", "d"',
// stripped: '["a","b c","d"',
// isExactPattern: false,
// isStartPattern: true
// }createPattern Function
Creates a pattern from an input string that matches that input string:
const input = `[1, 2, 3,
4, 5, 6]`;
createPattern(input); //=> '[1, 2, 3, 4, 5, 6]'
// we can limit the length of a pattern body
createPattern(input, 5); //=> '[1, 2 ...'
//return exact whole pattern if a short input string begins with a space
createPattern(' Hello World!'); //=> '" Hello World!"'
//return exact whole pattern if a short input string ends with a space
createPattern('Hello World! '); //=> '"Hello World! "'
//return exact start-pattern if a long input string begins with a space
createPattern(' Hello World!', 2); //=> '" He" ...'