npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

editer

v0.4.0

Published

A high level multiline string manipulation library

Downloads

10,129

Readme

editer

Build Status

A high level multiline string manipulation library.

What it does

Using editer, you can:

  • insert a string before/after a certain line in a multiline string optionally as a new line
  • insert a string before/after nth match of a regex in a multiline string optionally as a new line
  • remove a string from a multiline string before/after a regex matches, optionally from the same line and/or multiple times.

Installation

npm install --save editer

Usage

Here are some examples of what editer can do.

Example 1

Insert a new line after a certain line number.

var editer = require('editer');
var target = 'line 1\nline 3';

var result = editer.insert('line 2', target, {after: {line: 1}, asNewLine: true});
console.log(result);
// => 'line 1\nline 2\nline 3';

Example 2

Insert a string after the second occurrence of regex.

import create from './create';
import generate from './generate';

export {
  create
  generate
};
var fs = require('fs');
var editer = require('editer');
var target = fs.readFileSync('./target');

var result = editer.insert("import modify from './modify'", target, {
  after: {
    regex: /import .*$/,
    occurrence: 2,
  },
  asNewLine: true
});
console.log(result);
// import create from './create';
// import generate from './generate';
// import modify from './modify';
//
// export {
//   create
//   generate
// };

API

insert(string, target, options)

Inserts string to target at the position specified by options and returns the modified target. If options fails to find the desired position, returns undefined.

string

  • type: String
  • a string to be inserted at the target

target

  • type: String
  • a string to be modified by inserting string

options

  • type: Object
  • an object specifying the position at which the string is to be inserted to target and how it should be inserted (e.g. as a new line or a regular line).

At top level, it can have the following keys:

  • before
  • after
  • or
  • asNewLine

before, after

An object with either before or after is a 'condition'. A condition cannot have both before and after. They take as value an object with following possible key-value pairs.

line

  • type: Number
  • the line number in the target

regex

  • type: RegExp
  • the regex to be matched in the target. You may optionally provide occurrence if providing this option. See below. All regex should have global flag, as Editer uses lcoater internally.

occurrence

  • type: Number
  • the order of occurrence after which target is to be modified. If not provided, target is modified at the first occurrence of the regex.

last

  • type: Boolean
  • if set to true, modify the target at last occurrence of the regex.

Example

var target = "I love you\nHoney Bunny.";
var options = {before: {regex: /[a-zA-z]{5}/g, occurrence: 2}};
var result = editer.insert("Nooby ", target, options);
console.log(result);
// => "I love you\nHoney Nooby Bunny."

var target = "Whoa, whoa, whoa, whoa... stop right there.";
var options = {before: {regex: /whoa/ig, last: true}};
var result = editer.insert("my god, ", target, options);
console.log(result);
// => "Whoa, whoa, whoa, my god, whoa... stop right there."

or

or is an array of conditions. Editer attempts to use the conditions sequentially from the first to the last. If a condition matches, Editer ignores the rest of the conditions.

Example

var target = "Whoa, whoa, whoa, whoa... stop right there.";
var options = {or: [
  {before: {regex: /unicorn/ig, last: true}},
  {after: {regex: /whoa,\s/ig, occurrence: 3}},
  {after: {regex: /stop/i}}
]};
var result = editer.insert("hey, ", target, options);
console.log(result);
// => "Whoa, whoa, whoa, hey, whoa... stop right there."

asNewLine

  • type: Boolean
  • default: false
  • insert the string as a new line to the target.

Example

var target = "It's Zed's.\nWho's Zed?";
var options = {before: {regex: /Zed.*\n/g}, asNewLine: true};
var result = editer.insert("...", target, options);
console.log(result);
// => "It's \n...\nZed's.\nWho's Zed?"

remove(string, target, options)

Removes string from the target at the position specified by options.

string

  • type: String
  • a string to be removed from the target

target

  • type: String
  • a string to be modified by removing string

options

  • type: Object
  • an object specifying the position at which the string is to be removed from target.

At top level, it can have the following keys:

  • before
  • after
  • or
  • multi
  • onSameLine

All before, after, and or options are similar to those of insert API. The only difference is that here they support only regex, not line number.

multi

  • type: Boolean
  • default: false
  • Remove all occurrences of the string in the section of the target scoped by the condition, and other options such as onSameLine.

onSameLine

  • type: Boolean
  • default: false
  • Scope the removal to the same line as the match of the regex in the target.

License

MIT