{"id":25883144,"url":"https://github.com/maxlath/line-apply","last_synced_at":"2025-03-02T16:31:35.061Z","repository":{"id":184577524,"uuid":"651933428","full_name":"maxlath/line-apply","owner":"maxlath","description":"A CLI tool to transform a text stream by applying a JS function to each line","archived":false,"fork":false,"pushed_at":"2023-06-10T14:43:17.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-22T17:36:43.353Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxlath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-10T14:42:47.000Z","updated_at":"2023-06-10T14:43:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"b07dea22-9984-4f85-890a-c5249cd1b3dd","html_url":"https://github.com/maxlath/line-apply","commit_stats":null,"previous_names":["maxlath/line-apply"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fline-apply","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fline-apply/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fline-apply/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fline-apply/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxlath","download_url":"https://codeload.github.com/maxlath/line-apply/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241537336,"owners_count":19978514,"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":"2025-03-02T16:31:34.465Z","updated_at":"2025-03-02T16:31:35.025Z","avatar_url":"https://github.com/maxlath.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# line-apply\nA CLI tool to transform a text stream by applying a JS function to each line\n\nFeatures:\n* take the JS function to apply from a file\n* the function may return async results\n* preview the transformation results with the `--diff` option\n\n[![NPM](https://nodei.co/npm/line-apply.png?stars\u0026downloads\u0026downloadRank)](https://npmjs.com/package/line-apply/)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Node](https://img.shields.io/badge/node-%3E=%20v7.6.0-brightgreen.svg)](http://nodejs.org)\n\n\n## Summary\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Install](#install)\n- [How To](#how-to)\n  - [Basic](#basic)\n  - [Async](#async)\n  - [Diff mode](#diff-mode)\n  - [Filter mode](#filter-mode)\n  - [Use sub-function](#use-sub-function)\n  - [Pass additional arguments](#pass-additional-arguments)\n- [See also](#see-also)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n\n## Install\n```sh\nnpm i -g line-apply\n```\n\n## How To\n\n### Basic\n```sh\ncat some_data | line-apply some_transform_function.js \u003e some_data_transformed\n# Which can also be written\nline-apply some_transform_function.js \u003c cat some_data \u003e some_data_transformed\n```\nwhere `some_transform_function.js` just needs to export a JS function. This should work both with the ESM export syntax\n```js\n// some_transform_function.js\nexport default function (line) {\n  if (line.length \u003e 10) {\n    return line.toUpperCase()\n  } else {\n    // returning null or undefined drops the entry\n  }\n}\n```\nor with the CommonJS export syntax\n```js\n// some_transform_function.js\nmodule.exports = function (line) {\n  if (line.length \u003e 10) {\n    return line.toUpperCase()\n  } else {\n    // returning null or undefined drops the entry\n  }\n}\n```\n\n### Async\nThat function can also be async:\n```js\nimport { getSomeExtraData } from './path/to/get_some_extra_data.js'\n\n// some_async_transform_function.js\nexport default async function (doc) {\n  if (line.length \u003e 10) {\n    const id = line.match(/(Q[\\d+]) /)[1]\n    const data = await getSomeExtraData(id)\n    return `${line} ${data}`\n  } else {\n    // returning null or undefined drops the entry\n  }\n}\n```\n\n### Diff mode\nAs a way to preview the results of your transformation, you can use the diff mode\n```sh\ncat some_data | line-apply some_transform_function.js --diff\n```\nwhich will display a colored diff of each line before and after transformation.\n\n### Filter mode\nUse the js function only to filter lines: lines returning `true` will be let through. No transformation will be applied.\n```sh\ncat some_data | line-apply some_transform_function.js --filter\n```\n\n### Use sub-function\nGiven a `function_collection.js` file like:\n```js\n// function_collection.js\nexport function uppercase (line) {\n  return line.toUpperCase()\n}\n\nexport function lowercase (line) {\n  return line.toLowerCase()\n}\n```\n\nYou can use those subfunction by passing their key as an additional argument\n```sh\ncat some_data | line-apply ./function_collection.js uppercase\ncat some_data | line-apply ./function_collection.js lowercase\n```\n\nThis should also work with the CommonJS syntax:\n```js\n// function_collection.cjs\nmodule.exports = {\n  uppercase: line =\u003e line.toUpperCase(),\n  lowercase: line =\u003e line.toLowerCase(),\n}\n```\n\n### Pass additional arguments\nAny remaining argument will be passed to the function\n```sh\n# Pass '123' as argument to the exported function\ncat some_data | line-apply ./function.js 123\n# Pass '123' as argument to the exported sub-function foo\ncat some_data | line-apply ./function_collection.js foo 123\n```\n\n## See also\n* [ndjson-apply](https://github.com/maxlath/ndjson-apply/)\n* [json-apply](https://github.com/maxlath/json-apply/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlath%2Fline-apply","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxlath%2Fline-apply","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlath%2Fline-apply/lists"}