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).
Add this library to your package.json configuration:
var dialrules = ;var result = dialrules;
See below to know how the
rules variable should be formed. The
variable is an object that contains the information for the matched rules and
rule JSON Object is documented in the source code for
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
- value: String. Some conditions will use this, for example
- 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.
- 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.
- prepend: Prepends the given
valueto the dialed string.
- append: Appends the given
valueto the dialed string.
- replace: Replaces the given
- no_change: Does nothing. Useful when combined with
lastto avoid matching further rules.
- remove: Removes
condition_patternfrom the dialed string.
Below you will find a few examples for the most common usage. More examples can be found in the tests.
Let's say your US carrier will send you US numbers without the leading
international numbers with a
011 and you want to transform any of those into
condition: 'length'description: 'for US numbers prepend 1'condition_pattern: '10'action: 'prepend'value: '1'last: truecondition: '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'
Changing a specific number
condition: 'exact_match'description: 'Changes phone number'condition_pattern: '12223334444'value: '15556667777'action: 'replace'
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
- 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.
npm run buildand make sure everything is ok before submitting the pull request (make eslint happy).
The source code is released under Apache 2 License.
Check LICENSE file for more information.