custom-translate
Advanced tools
Comparing version 2.1.1 to 2.2.0
20
index.js
@@ -1,4 +0,4 @@ | ||
const { version } = require('./package'); | ||
exports.version = require('./package').version; | ||
const wordTrans = (text, dict, join = ' ') => { | ||
exports.wordTrans = (text, dict, join = ' ') => { | ||
if (typeof text !== 'string') throw new TypeError('text must be a string.'); | ||
@@ -19,3 +19,3 @@ if (typeof dict !== 'object') throw new TypeError('dictionary must be an object.'); | ||
const letterTrans = (text, dict, join = '') => { | ||
exports.letterTrans = (text, dict, join = '') => { | ||
if (typeof text !== 'string') throw new TypeError('text must be a string.'); | ||
@@ -26,6 +26,12 @@ if (typeof dict !== 'object') throw new TypeError('dictionary must be an object.'); | ||
module.exports = { | ||
version, | ||
wordTrans, | ||
letterTrans | ||
exports.regexTrans = (text, dict, flags = 'gi') => { | ||
if (typeof text !== 'string') throw new TypeError('text must be a string.'); | ||
if (typeof dict !== 'object') throw new TypeError('dictionary must be an object.'); | ||
if (typeof flags !== 'string') throw new TypeError('flags must be a string.'); | ||
for (const expression of Object.keys(dict)) { | ||
const replacement = dict[expression]; | ||
const regex = new RegExp(expression, flags); | ||
text = text.replace(regex, replacement); | ||
} | ||
return text; | ||
}; |
{ | ||
"name": "custom-translate", | ||
"version": "2.1.1", | ||
"description": "A simple module for translating certain words or letters in a string with other words or letters.", | ||
"version": "2.2.0", | ||
"description": "A simple module for replacing words, letters, or patterns in a string with others.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -6,6 +6,7 @@ # custom-translate | ||
custom-translate is a simple module for translating certain words or letters in | ||
a string with other words or letters you provide. Usage is simple: | ||
custom-translate is a simple module for translating certain words, letters, or | ||
patterns in a string with others. Usage is simple: | ||
#### wordTrans | ||
wordTrans takes an object of words and changes them into others. For example: | ||
```js | ||
@@ -22,4 +23,3 @@ const translator = require('custom-translate'); | ||
``` | ||
Output will be: | ||
`I have a cat that goes meow.` | ||
Output will be: `I have a cat that goes meow.` | ||
@@ -33,2 +33,4 @@ `wordTrans` automatically ignores casing, all instances of the word, regardless | ||
#### letterTrans | ||
letterTrans takes an object of letters (or symbols) and changes them into | ||
others. For example: | ||
```js | ||
@@ -46,4 +48,3 @@ const translator = require('custom-translate'); | ||
Output will be: | ||
`S like !heese` | ||
Output will be: `S like !heese` | ||
@@ -59,1 +60,25 @@ Unlike `wordTrans`, `letterTrans` does not ignore casing by default. | ||
``` | ||
#### regexTrans | ||
regexTrans takes an object with Regular Expressions (as strings) as the keys and | ||
what they are to be replaced with as the values. They will be replaced in order. | ||
For example: | ||
```js | ||
const translator = require('custom-translate'); | ||
const text = 'ABC abc 123'; | ||
const dictionary = { | ||
'ABCD?': 'EFG', | ||
'123': '456' | ||
}; | ||
translator.regexTrans(text, dictionary); | ||
``` | ||
Output will be: `EFG EFG 456` | ||
You can also change the flags to use during replacements. This defaults to `gi`. | ||
```js | ||
translator.regexTrans(text, dictionary, 'i'); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5766
32
80