{"id":15502082,"url":"https://github.com/sungwoncho/editer","last_synced_at":"2025-04-22T22:33:17.314Z","repository":{"id":57219579,"uuid":"52150048","full_name":"sungwoncho/editer","owner":"sungwoncho","description":"A high level multiline string manipulation library","archived":false,"fork":false,"pushed_at":"2016-02-27T09:30:32.000Z","size":31,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T14:18:48.407Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sungwoncho.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-20T11:28:23.000Z","updated_at":"2024-05-16T16:30:54.000Z","dependencies_parsed_at":"2022-08-29T00:11:33.296Z","dependency_job_id":null,"html_url":"https://github.com/sungwoncho/editer","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sungwoncho%2Fediter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sungwoncho%2Fediter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sungwoncho%2Fediter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sungwoncho%2Fediter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sungwoncho","download_url":"https://codeload.github.com/sungwoncho/editer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250333900,"owners_count":21413474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T09:07:48.785Z","updated_at":"2025-04-22T22:33:17.293Z","avatar_url":"https://github.com/sungwoncho.png","language":"JavaScript","readme":"# editer\n\n[![Build Status](https://travis-ci.org/sungwoncho/editer.svg?branch=master)](https://travis-ci.org/sungwoncho/editer)\n\nA high level multiline string manipulation library.\n\n\n## What it does\n\nUsing editer, you can:\n\n* insert a string before/after a certain line in a multiline string optionally\nas a new line\n* insert a string before/after nth match of a regex in a multiline string\noptionally as a new line\n* remove a string from a multiline string before/after a regex matches,\noptionally from the same line and/or multiple times.\n\n## Installation\n\n    npm install --save editer\n\n\n## Usage\n\nHere are some examples of what editer can do.\n\n\n### Example 1\n\nInsert a new line after a certain line number.\n\n```js\nvar editer = require('editer');\nvar target = 'line 1\\nline 3';\n\nvar result = editer.insert('line 2', target, {after: {line: 1}, asNewLine: true});\nconsole.log(result);\n// =\u003e 'line 1\\nline 2\\nline 3';\n```\n\n### Example 2\n\nInsert a string after the second occurrence of regex.\n\n```target.js\nimport create from './create';\nimport generate from './generate';\n\nexport {\n  create\n  generate\n};\n```\n\n```js\nvar fs = require('fs');\nvar editer = require('editer');\nvar target = fs.readFileSync('./target');\n\nvar result = editer.insert(\"import modify from './modify'\", target, {\n  after: {\n    regex: /import .*$/,\n    occurrence: 2,\n  },\n  asNewLine: true\n});\nconsole.log(result);\n// import create from './create';\n// import generate from './generate';\n// import modify from './modify';\n//\n// export {\n//   create\n//   generate\n// };\n\n```\n\n## API\n\n### insert(string, target, options)\n\nInserts `string` to `target` at the position specified by `options` and returns\nthe modified `target`. If `options` fails to find the desired position, returns\n`undefined`.\n\n**string**\n\n* type: `String`\n* a string to be inserted at the target\n\n**target**\n\n* type: `String`\n* a string to be modified by inserting `string`\n\n**options**\n\n* type: `Object`\n* an object specifying the position at which the `string` is to be inserted to\n`target` and how it should be inserted (e.g. as a new line or a regular line).\n\nAt top level, it can have the following keys:\n\n* `before`\n* `after`\n* `or`\n* `asNewLine`\n\n#### before, after\n\nAn object with either `before` or `after` is a 'condition'. A condition cannot\nhave both `before` and `after`. They take as value an object with following\npossible key-value pairs.\n\n**line**\n\n* type: `Number`\n* the line number in the target\n\n**regex**\n\n* type: `RegExp`\n* the regex to be matched in the target. You may optionally provide `occurrence`\nif providing this option. See below. All regex should have **global** flag, as\nEditer uses [lcoater](https://github.com/sungwoncho/locater) internally.\n\n**occurrence**\n\n* type: `Number`\n* the order of occurrence after which `target` is to be modified. If not\nprovided, `target` is modified at the first occurrence of the `regex`.\n\n**last**\n\n* type: `Boolean`\n* if set to true, modify the `target` at last occurrence of the regex.\n\n*Example*\n\n```js\nvar target = \"I love you\\nHoney Bunny.\";\nvar options = {before: {regex: /[a-zA-z]{5}/g, occurrence: 2}};\nvar result = editer.insert(\"Nooby \", target, options);\nconsole.log(result);\n// =\u003e \"I love you\\nHoney Nooby Bunny.\"\n\nvar target = \"Whoa, whoa, whoa, whoa... stop right there.\";\nvar options = {before: {regex: /whoa/ig, last: true}};\nvar result = editer.insert(\"my god, \", target, options);\nconsole.log(result);\n// =\u003e \"Whoa, whoa, whoa, my god, whoa... stop right there.\"\n```\n\n#### or\n\n`or` is an array of conditions. Editer attempts to use the conditions\nsequentially from the first to the last. If a condition matches, Editer ignores\nthe rest of the conditions.\n\n*Example*\n\n```js\nvar target = \"Whoa, whoa, whoa, whoa... stop right there.\";\nvar options = {or: [\n  {before: {regex: /unicorn/ig, last: true}},\n  {after: {regex: /whoa,\\s/ig, occurrence: 3}},\n  {after: {regex: /stop/i}}\n]};\nvar result = editer.insert(\"hey, \", target, options);\nconsole.log(result);\n// =\u003e \"Whoa, whoa, whoa, hey, whoa... stop right there.\"\n```\n\n#### asNewLine\n\n* type: `Boolean`\n* default: `false`\n* insert the `string` as a new line to the `target`.\n\n*Example*\n\n```js\nvar target = \"It's Zed's.\\nWho's Zed?\";\nvar options = {before: {regex: /Zed.*\\n/g}, asNewLine: true};\nvar result = editer.insert(\"...\", target, options);\nconsole.log(result);\n// =\u003e \"It's \\n...\\nZed's.\\nWho's Zed?\"\n```\n\n### remove(string, target, options)\n\nRemoves `string` from the `target` at the position specified by `options`.\n\n**string**\n\n* type: `String`\n* a string to be removed from the target\n\n**target**\n\n* type: `String`\n* a string to be modified by removing `string`\n\n**options**\n\n* type: `Object`\n* an object specifying the position at which the `string` is to be removed from\n`target`.\n\nAt top level, it can have the following keys:\n\n* `before`\n* `after`\n* `or`\n* `multi`\n* `onSameLine`\n\nAll `before`, `after`, and `or` options are similar to those of `insert` API.\nThe only difference is that here they support only regex, not line number.\n\n#### multi\n\n* type: `Boolean`\n* default: `false`\n* Remove all occurrences of the `string` in the section of the `target` scoped\nby the condition, and other options such as `onSameLine`.\n\n#### onSameLine\n\n* type: `Boolean`\n* default: `false`\n* Scope the removal to the same line as the match of the regex in the target.\n\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsungwoncho%2Fediter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsungwoncho%2Fediter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsungwoncho%2Fediter/lists"}