Skip to content

Would be good to document how to do a function replace using a regexp  #49

@davestewart

Description

@davestewart

I tried to use your library using a regexp find and function replace, but spent 20 minutes getting my head round the options and console logging stuff to see what I needed to do. Eventually I gave up and I ended up writing a glob and doing it myself.

This was my use case - converting tags (with ml- prefix) from snake-case to PascalCase:

const Fs = require('fs')
const glob = require('glob')

// options
const options = { encoding:'utf8' }

// start
glob('./src/**/*.vue', function (er, files) {
  // debug
  console.log(`Processing ${files.length} files...`)

  // process files
  files.forEach(file => {
    // variables
    let count = 0
    const text = Fs.readFileSync(file, options)

    // replacement
    const output = text.replace(/<\/?ml-[\w-]+/g, function (match, pos, all) {
      count += 1
      return match
        .replace(/-\w/g, m => m.slice(1).toUpperCase())
        .replace('ml', 'Ml')
    })

    // if replacements were made
    if (count) {
      Fs.writeFileSync(file, output, options)
      console.log(count, file)
    }
  })
})

With your lib I just didn't know what the replace function's arguments were, so specifically documenting an example would make it less ambiguous.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions