{"id":15629697,"url":"https://github.com/anko/slice-with-context","last_synced_at":"2025-03-29T18:15:26.016Z","repository":{"id":150123051,"uuid":"299670528","full_name":"anko/slice-with-context","owner":"anko","description":"JS module: slice string, but with context (useful for showing parse errors with context)","archived":false,"fork":false,"pushed_at":"2020-09-30T13:25:47.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-04T19:14:41.706Z","etag":null,"topics":["context","parser","slice","string","utility-function"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anko.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-29T16:10:54.000Z","updated_at":"2020-09-30T13:25:50.000Z","dependencies_parsed_at":"2023-05-11T20:00:43.559Z","dependency_job_id":null,"html_url":"https://github.com/anko/slice-with-context","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fslice-with-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fslice-with-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fslice-with-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anko%2Fslice-with-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anko","download_url":"https://codeload.github.com/anko/slice-with-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246223325,"owners_count":20743168,"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":["context","parser","slice","string","utility-function"],"created_at":"2024-10-03T10:28:11.916Z","updated_at":"2025-03-29T18:15:25.988Z","avatar_url":"https://github.com/anko.png","language":"JavaScript","readme":"# slice-with-context\u0026emsp;[![](https://img.shields.io/npm/v/slice-with-context.svg?style=flat-square)](https://www.npmjs.com/package/slice-with-context) [![](https://img.shields.io/travis/anko/slice-with-context.svg?style=flat-square)](https://travis-ci.org/anko/slice-with-context) [![](https://img.shields.io/david/anko/slice-with-context?style=flat-square)](https://david-dm.org/anko/slice-with-context)\n\nBasically `String.prototype.slice`, but with a configurably-positioned window\naround it, showing context.\n\n    npm i slice-with-context\n\nThe module exports 1 function:\n\n## `sliceWithContext(inputString, windowSize, cutOffset, cutLength [, windowLeftBias] [, overflowLeftBias])`\n\nParams:\n\n - `inputString`:`String`\u0026emsp;string to cut\n - `windowSize`:`Number`\u0026emsp;size of window\n - `cutOffset`:`Number`\u0026emsp;cut start position, in characters\n - `cutLength`:`Number`\u0026emsp;cut length, in characters\n - `windowLeftBias`:`Number` between 0–1 [*optional*]\u0026emsp;if there is space\n   left in the window on both sides of the sliced part, this option determines\n   what proportion of what's left to show on the left rather than the right.\n\n   0 means only show context on the left. 1 means only show context on the\n   right. (default 0.5)\n\n - `overflowLeftBias`:`Number`between 0–1 [*optional*]\u0026emsp;if the window is\n   too small to show the sliced part fully, this option determines which part\n   of the sliced part should be shown.\n\n   0 means only show the very start of the slice. 1 means only show the very\n   end of the slice.  Intermediate values mean to show that far along the\n   middle of the slice.  (default 0)\n\nThrows if the slice parameters would miss the string entirely, by being too\nlong, offset past it, or other such obviously wrong nonsense.\n\nClamps `windowLeftBias` and `overflowLeftBias` to 0–1.\n\n\u003c!-- !test program\n  # Replace `require` with current directory, then run with node.\n  node -e \"\n  const stdin = require('fs').readFileSync(0, 'utf-8')\n  process.stdout.write(\n    stdin.replace(\n      /require\\('slice-with-context'\\)/g,\n      'require(\\'.\\')'))\n  \" | node\n--\u003e\n\n## Example\n\nSuppose you want to slice the bold part out of 1234\u003cb\u003e5678\u003c/b\u003e9, showing a\ntotal of 6 characters; the sliced part and the rest just for context because\nyou have space to spare.\n\n\u003c!-- !test in example --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6,    // windowSize\n  4,    // offset\n  4,    // length\n  0.5, // windowLeftBias\n  0,    // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out example --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '456789',\n\u003e   visibleSlice: '5678',\n\u003e   fullSlice: '5678',\n\u003e   position: { offset: 1, length: 4 }\n\u003e }\n\u003e ```\n\nIf you're writing a compiler or something, you might render that as—\n\n    456789\n     ^^^^\n\nOr use ANSI colour codes, or some such.\n\n## Specific situations\n\n\u003cdetails\u003e\u003csummary\u003e\nEqual amounts of context on either side\n\u003c/summary\u003e\n\n\u003c!-- !test in basic --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6,   // windowSize\n  4,   // offset\n  4,   // length\n  0.5, // windowLeftBias\n  0,   // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out basic --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '456789',\n\u003e   visibleSlice: '5678',\n\u003e   fullSlice: '5678',\n\u003e   position: { offset: 1, length: 4 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nWith context only on the left\n\u003c/summary\u003e\n\n\u003c!-- !test in left context only --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  4, // offset\n  4, // length\n  1, // windowLeftBias\n  0, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out left context only--\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '345678',\n\u003e   visibleSlice: '5678',\n\u003e   fullSlice: '5678',\n\u003e   position: { offset: 2, length: 4 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nWith context only on the right\n\u003c/summary\u003e\n\n\u003c!-- !test in right context only --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  4, // offset\n  4, // length\n  0, // windowLeftBias\n  0, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out right context only--\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '567890',\n\u003e   visibleSlice: '5678',\n\u003e   fullSlice: '5678',\n\u003e   position: { offset: 0, length: 4 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nLeft context only, but with no space\n\u003c/summary\u003e\n\n\u003c!-- !test in left context with no space --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  0, // offset\n  4, // length\n  1, // windowLeftBias\n  0, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out left context with no space --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '123456',\n\u003e   visibleSlice: '1234',\n\u003e   fullSlice: '1234',\n\u003e   position: { offset: 0, length: 4 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nBoth-sided context, but no space at end\n\u003c/summary\u003e\n\n\u003c!-- !test in both sides context but at end --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  6, // offset\n  4, // length\n  0.5, // windowLeftBias\n  0, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out both sides context but at end --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: true,\n\u003e   withContext: '567890',\n\u003e   visibleSlice: '7890',\n\u003e   fullSlice: '7890',\n\u003e   position: { offset: 2, length: 4 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nInsufficient space in window, truncated to show start\n\u003c/summary\u003e\n\n\u003c!-- !test in truncate to left --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  0, // offset\n  8, // length\n  1, // windowLeftBias\n  1, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out truncate to left --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: false,\n\u003e   withContext: '123456',\n\u003e   visibleSlice: '123456',\n\u003e   fullSlice: '12345678',\n\u003e   position: { offset: 0, length: 6 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\u003csummary\u003e\nInsufficient space in window, truncated to show end\n\u003c/summary\u003e\n\n\u003c!-- !test in truncate to right --\u003e\n\n```js\nconst sliceWithContext = require('slice-with-context')\nconst output = sliceWithContext(\n  '1234567890',\n  6, // windowSize\n  0, // offset\n  8, // length\n  1, // windowLeftBias\n  0, // overflowLeftBias\n)\nconsole.log(output)\n```\n\n\u003c!-- !test out truncate to right --\u003e\n\n\u003e ```\n\u003e {\n\u003e   fit: false,\n\u003e   withContext: '345678',\n\u003e   visibleSlice: '345678',\n\u003e   fullSlice: '12345678',\n\u003e   position: { offset: 0, length: 6 }\n\u003e }\n\u003e ```\n\u003c/details\u003e\n\n## Tests\n\nThat's the code examples above!  They're run with\n[txm](https://github.com/anko/txm).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanko%2Fslice-with-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanko%2Fslice-with-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanko%2Fslice-with-context/lists"}