Giter Site home page Giter Site logo

zbo14 / reworder Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 622 KB

✏️ Replace words and phrases via custom mappings

Home Page: https://www.npmjs.com/package/reworder

License: MIT License

JavaScript 99.57% Shell 0.43%
regex text text-processing matching subgroups words phrases text-matching

reworder's Introduction

reworder

npm js-standard-style

Replace words and phrases via custom mappings!

Install

npm i reworder

Usage

Replace by strings:

'use strict'

const reworder = require('reword')

const reword = reworder([
  { key: 'foo', value: 'bar' },
  { key: 'bar', value: 'baz' }
])

const input = 'abc foo hello world bar baz'
const result = reword(input)

console.log(result)

// {
//   input: 'abc foo hello world bar baz',
//
//   matches: [
//     { key: 'foo', index: 4, value: 'bar' },
//     { key: 'bar', index: 20, value: 'baz' }
//   ],
//
//   output: 'abc bar hello world baz baz'
// }

Replace by regex:

const reword = reworder({ key: /ba\w/, value: 'foo' })
const input = 'abc foo hello world bar baz'
const result = reword(input)

console.log(result)

// {
//   input: 'abc foo hello world bar baz',
//
//   matches: [
//     { key: 'bar', index: 20, value: 'foo' },
//     { key: 'baz', index: 24, value: 'foo' }
//   ],
//
//   output: 'abc foo hello world foo foo'
// }

Replace by transform function:

const reword = reworder({
  key: /ba\w/,
  transform: key => key.slice(-1)
})

const input = 'abc foo hello world bar baz'
const result = reword(input)

console.log(result)

// {
//   input: 'abc foo hello world bar baz',
//
//   matches: [
//     { key: 'bar', index: 20, value: 'r' },
//     { key: 'baz', index: 24, value: 'z' }
//   ],
//
//   output: 'abc foo hello world r z'
// }

transform can be async, in which case the reword function returns a promise:

const result = await reword(input)

Replace with options:

const config = [
  { key: 'foo', value: 'bar' },
  { key: 'hello world', value: 'helloworld' }
]

const options = {
  caseInsensitive: true,
  includePunctuation: ',',
  variableSpacing: true
}

const reword = reworder(config, options)
const input = 'abc FoO hello,   world bar baz'
const result = reword(input)

console.log(result)

// {
//   input: 'abc FoO hello,   world bar baz',
//
//   matches: [
//     { key: 'FoO', index: 4, value: 'bar' },
//     { key: 'hello,   world', index: 8, value: 'helloworld' }
//   ],
//
//   output: 'abc bar helloworld bar baz'
// }

Reference

  • config is an object literal or array of object literals. Each object literal must contain a key (string or RegExp) and either value (string) or transform (function). The transform function should accept a single argument- a string matching the key, and return a string or a promise that resolves to a string.
  • options is an object literal with the following properties:
    • caseInsensitive is a boolean indicating whether regex permits case insensitive matching.
    • includePunctuation is a boolean or string indicating whether/what punctuation can be included in matches.
    • variableSpacing is a boolean indicating whether the regex matches variable number of spaces.

Test

npm test

Lint

npm run lint or npm run lint:fix

License

Licensed under MIT.

reworder's People

Contributors

zbo14 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.