multisplice
easily splice a string multiple times, using offsets into the original string
Last updated 3 years ago by goto-bus-stop .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install multisplice 
SYNC missed versions from official npm registry.

multisplice

easily splice a string multiple times, using offsets into the original string

npm travis standard

Install

npm install multisplice

Usage

Renaming identifiers in Javascript code:

var multisplice = require('./')
var dedent = require('dedent')
var astw = require('astw')

var src = dedent`
  function beep (boop) {
    boop *= baz
    console.log(boop)
  }
  var baz = 10
  beep(17)
`

var splicer = multisplice(src)
var renames = {}
var nextName = 'a'.charCodeAt()
astw(src)(function (node) {
  if (node.type === 'Identifier') {
    if (!renames[node.name]) renames[node.name] = String.fromCharCode(nextName++)

    splicer.splice(node.start, node.end, renames[node.name])
  }
})

console.log(splicer.toString())
function a (b) {
  b *= c
  d.e(b)
}
var c = 10
a(17)

API

var s = multisplice(string)

Create a string splicer instance. This instance will splice things into string.

s.splice(start, end, value)

Replace the slice from indices start to end in the original string with value.

s.slice(start, end)

Get a part of the spliced string, using indices start to end from the original string.

s.toString()

Get the full spliced string (.slice(0)).

License

Apache-2.0

Current Tags

  • 1.0.0                                ...           latest (3 years ago)

1 Versions

  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 2
This Week 5
This Month 10
Last Day 0
Last Week 2
Last Month 10
Dependencies (0)
None
Dev Dependencies (4)

Copyright 2014 - 2017 © taobao.org |