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

dialrules

v0.0.2

Published

A library to bill telephone calls

Downloads

11

Readme

License npm version js-standard-style

Build Status Coverage Status Code Climate Issue Count

dialrules

This library is used to transform dialed strings. It's useful when for example you need to prepend, append, change, or remove a given string to a number before issuing a VoIP call (or when receiving the call).

Installing

Add this library to your package.json configuration:

  "dependencies": {
    "dialrules": "latest"
  }

Using it

  var dialrules = require('dialrules');
  var result = dialrules.translate(dialedString, rules);

See below to know how the rules variable should be formed. The result variable is an object that contains the information for the matched rules and final result.

Rules

The rule JSON Object is documented in the source code for index.js.

All rules have the following fields:

  • condition: String. One of the available conditions (see below).
  • condition_pattern: String. This is the pattern to be matched by the type of condition.
  • value: String. Some conditions will use this, for example replace.
  • description: String. Optional. Useful for debugging purposes. Allows you to add a description for this rule that will be passed on after the match ends.
  • last: Boolean. When true, no further rules will be matched if this one matches.

Conditions

  • starts_with: Check if the given needle is at the start of the dialed string.
  • ends_with: Checks if the dialed string ends with the given needle.
  • contains: Checks if the dialed string contains the given needle.
  • exact_match: Checks if the dialed string matches exactly with the given needle.
  • length: Checks if the dialed string length is between a given range.
  • custom: condition_pattern becomes a regular expression that you can create and pass on.

Actions

  • prepend: Prepends the given value to the dialed string.
  • append: Appends the given value to the dialed string.
  • replace: Replaces the given condition_pattern by value.
  • no_change: Does nothing. Useful when combined with last to avoid matching further rules.
  • remove: Removes condition_pattern from the dialed string.

Examples

Below you will find a few examples for the most common usage. More examples can be found in the tests.

US Carrier

Let's say your US carrier will send you US numbers without the leading 1 and international numbers with a 011 and you want to transform any of those into E.164:

[
  {
    condition: 'length',
    description: 'for US numbers prepend 1',
    condition_pattern: '10',
    action: 'prepend',
    value: '1',
    last: true
  },
  {
    condition: 'starts_with',
    description: 'for Intl numbers remove the US intl prefix',
    condition_pattern: '011',
    action: 'remove',
    last: true
  }
]

Adding a suffix

Now let's say that you have a carrier that allows you to choose the route quality by appending a string. You might want to use a specific route for all calls to Argentina:

[
  {
    condition: 'starts_with',
    description: 'Append route to Argentina',
    condition_pattern: '54',
    action: 'append',
    value: '#999#15'
  }
]

The exact opposite can be achieved by this

[
  {
    condition: 'ends_with',
    description: 'Removes route suffix',
    condition_pattern: '#999#15',
    action: 'remove'
  }
]

You can also strip a prefix like the one above while at the same capturing it with a custom condition

[
  {
    condition: 'custom',
    description: 'Removes and captures route prefix',
    condition_pattern: '^(\\d{3})#(\\d{2})#',
    action: 'remove'
  }
]

The result will contain the 2 captured groups in the field captures and the transformed number will end up without the prefix.

Changing a specific number

[
  {
    condition: 'exact_match',
    description: 'Changes phone number',
    condition_pattern: '12223334444',
    value: '15556667777'
    action: 'replace'
  }
]

Developers

This project uses standard npm scripts. Current tasks include:

  • test: Runs Mocha tests.
  • jsdoc: Runs JSDoc3.
  • eslint: Runs ESLint.
  • coverage: Runs the tests and then Instanbul to get a coverage report.
  • build: This is the default task, and will run all the other tasks.

Running an npm task

To run a task, just do:

npm run build

Contributing

To contribute:

  • Make sure you open a concise and short pull request.
  • Throw in any needed unit tests to accomodate the new code or the changes involved.
  • Run npm run build and make sure everything is ok before submitting the pull request (make eslint happy).
  • Your code must comply with the Javascript Standard Style, ESLint should take care of that.

License

The source code is released under Apache 2 License.

Check LICENSE file for more information.