npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loose-string-test

v1.0.1

Published

Loosely tests strings with ease, omits unnecessary spaces.

Downloads

22

Readme

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]'); // true

Also 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'); // true

Whitespaces 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 }'); // true

Loosely 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));
//=> true

Installation

$ npm i loose-string-test

Usage

Typescript / ES module:

import {looseStringTest} from 'loose-string-test';

Javascript / CommonJS:

const {looseStringTest} = require('loose-string-test');

About Lib

  • Typed
  • With d.ts for 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.

  1. exact pattern matches the exact the same input
  2. loose pattern matches the input, even if there are some whitespace difference
  3. whole pattern matches the input as a whole
  4. 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'); //=> true

With 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'); //=> false

With 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. true

Loose Whole Pattern

loose whole pattern ignores spaces and EOLs:

// looseStringTest(pattern: string, input: string)
looseStringTest('abcd', 'abcd'); //=> true
looseStringTest(' a bc d ', ' ab  cd'); //=> true

It 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 ');
// ... true

The only important whitespaces are ones inside quote pairs in the loose pattern:

looseStringTest(' [ "a", " b " ] ', '["a"," b "]'); // true

Loose 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)
);
// ... true

The only important whitespaces are ones inside quote pairs in the loose start pattern:

looseStringTest(' [ "a", " b ", .... ', '["a"," b ","c "]'); // true

parsePattern 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" ...'