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

coerce-string

v0.1.0

Published

A utility to make a string conform to a pattern

Downloads

1,249

Readme

String Coercion

This tool will match a source string to a pattern as closely as it can manage. This can be useful for use in html input masking or in formatting strings for display. This tries to be as unopinionated as possible, so implementation into any modern framework or workflow should be very easy. A pre-built react component might be added later on.

Examples: https://jamalneufeld.github.io/coerce-string/

Basic Usage

Install

npm install --save coerce-string

Import

import Coerce from 'coerce-string';

Use

const settings = {
    value: '02062016',
    pattern: '99/99/9999',
}
Coerce.string(settings);
// result: '02/06/2016'

Built-in regex

You can use some built-in "special characters" in your pattern. These use regex to match characters:

A: '[A-Z]'
9: '[0-9]'
*: '.'
+: '.*'

Special Note: Only use + at the end of a pattern.

Examples

Date: '99/99/9999'

Phone: '(999) 999-9999'

Credit Card: '9999 9999 9999 9999'

Web Address: 'http://+'

Custom regex

In some cases you'll want to expand on the built-in types. The Web address example for instance would restrict your string to non-ssl addresses. To add custom special characters just add a extend object to the settings object:

const settings = {
    pattern: 'httpS://+',
    extend: {
        S: 's?',
        5: '.{0,5}'
    }
}
const coerce = new Coerce(settings);

coerce.string('http');
// result: 'http'

coerce.string('httpabc');
// result: 'http://abc'

coerce.string('httpsabc');
// result: 'https://abc'

coerce.string('https');
// result: 'https://'

coerce.string('abc');
// result: 'http://abc'

coerce.string('sabc');
// result: 'https://abc'

coerce.string('sabcdef');
// result: 'https://abcde'

Use in html input

When the input value reaches the end with a match, but there's still more pattern to go, the tool will try to fill it out:

Coerce.string({
    value: '212',
    pattern: '999-999-9999',
});
// result '212-'

This is by design, it makes filling in masked inputs easier and less confusing, but causes an issue if you're using this as-you-type since backspace will be impossible and overwritten by the added character. For this use case, you should instantiate the class:

const coerceInstance = new Coerce({
    pattern: '999-999-9999',
})

Now let's have our user paste in 21223

let string = coerceInstance.string('21223');
// result '212-23'

Now, the user realizes they wanted to change the 3rd digit, and proceeds to hit backspace 4 times:

string = coerceInstance.string('212-2'); // result: '212-2'
string = coerceInstance.string('212-'); // result: '212-'
string = coerceInstance.string('212'); // result: '212'
string = coerceInstance.string('21'); // result: '21'

The user can now re-enter the number. This is a very common use case and works with the instantiated version, but without it here's what happens:

string = Coerce.string('212-2'); // result: '212-2'
string = Coerce.string('212-'); // result: '212-'
string = Coerce.string('212'); // result: '212-'

And as you can see the user would be stuck with that dash. Make sure to instantiate if you're going to be using this for input masking.