dialrules
v0.0.2
Published
A library to bill telephone calls
Downloads
11
Maintainers
Readme
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
byvalue
. - 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.