https://github.com/anko/slice-with-context
JS module: slice string, but with context (useful for showing parse errors with context)
https://github.com/anko/slice-with-context
context parser slice string utility-function
Last synced: 10 months ago
JSON representation
JS module: slice string, but with context (useful for showing parse errors with context)
- Host: GitHub
- URL: https://github.com/anko/slice-with-context
- Owner: anko
- License: isc
- Created: 2020-09-29T16:10:54.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-30T13:25:47.000Z (over 5 years ago)
- Last Synced: 2025-02-04T19:14:41.706Z (12 months ago)
- Topics: context, parser, slice, string, utility-function
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# slice-with-context [](https://www.npmjs.com/package/slice-with-context) [](https://travis-ci.org/anko/slice-with-context) [](https://david-dm.org/anko/slice-with-context)
Basically `String.prototype.slice`, but with a configurably-positioned window
around it, showing context.
npm i slice-with-context
The module exports 1 function:
## `sliceWithContext(inputString, windowSize, cutOffset, cutLength [, windowLeftBias] [, overflowLeftBias])`
Params:
- `inputString`:`String` string to cut
- `windowSize`:`Number` size of window
- `cutOffset`:`Number` cut start position, in characters
- `cutLength`:`Number` cut length, in characters
- `windowLeftBias`:`Number` between 0–1 [*optional*] if there is space
left in the window on both sides of the sliced part, this option determines
what proportion of what's left to show on the left rather than the right.
0 means only show context on the left. 1 means only show context on the
right. (default 0.5)
- `overflowLeftBias`:`Number`between 0–1 [*optional*] if the window is
too small to show the sliced part fully, this option determines which part
of the sliced part should be shown.
0 means only show the very start of the slice. 1 means only show the very
end of the slice. Intermediate values mean to show that far along the
middle of the slice. (default 0)
Throws if the slice parameters would miss the string entirely, by being too
long, offset past it, or other such obviously wrong nonsense.
Clamps `windowLeftBias` and `overflowLeftBias` to 0–1.
## Example
Suppose you want to slice the bold part out of 123456789, showing a
total of 6 characters; the sliced part and the rest just for context because
you have space to spare.
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
4, // offset
4, // length
0.5, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '456789',
> visibleSlice: '5678',
> fullSlice: '5678',
> position: { offset: 1, length: 4 }
> }
> ```
If you're writing a compiler or something, you might render that as—
456789
^^^^
Or use ANSI colour codes, or some such.
## Specific situations
Equal amounts of context on either side
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
4, // offset
4, // length
0.5, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '456789',
> visibleSlice: '5678',
> fullSlice: '5678',
> position: { offset: 1, length: 4 }
> }
> ```
With context only on the left
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
4, // offset
4, // length
1, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '345678',
> visibleSlice: '5678',
> fullSlice: '5678',
> position: { offset: 2, length: 4 }
> }
> ```
With context only on the right
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
4, // offset
4, // length
0, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '567890',
> visibleSlice: '5678',
> fullSlice: '5678',
> position: { offset: 0, length: 4 }
> }
> ```
Left context only, but with no space
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
0, // offset
4, // length
1, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '123456',
> visibleSlice: '1234',
> fullSlice: '1234',
> position: { offset: 0, length: 4 }
> }
> ```
Both-sided context, but no space at end
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
6, // offset
4, // length
0.5, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: true,
> withContext: '567890',
> visibleSlice: '7890',
> fullSlice: '7890',
> position: { offset: 2, length: 4 }
> }
> ```
Insufficient space in window, truncated to show start
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
0, // offset
8, // length
1, // windowLeftBias
1, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: false,
> withContext: '123456',
> visibleSlice: '123456',
> fullSlice: '12345678',
> position: { offset: 0, length: 6 }
> }
> ```
Insufficient space in window, truncated to show end
```js
const sliceWithContext = require('slice-with-context')
const output = sliceWithContext(
'1234567890',
6, // windowSize
0, // offset
8, // length
1, // windowLeftBias
0, // overflowLeftBias
)
console.log(output)
```
> ```
> {
> fit: false,
> withContext: '345678',
> visibleSlice: '345678',
> fullSlice: '12345678',
> position: { offset: 0, length: 6 }
> }
> ```
## Tests
That's the code examples above! They're run with
[txm](https://github.com/anko/txm).