@arcsine/nodesh
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -102,3 +102,3 @@ /// <reference types="node" /> | ||
* .$read() // Read a file | ||
* .$match($pattern.URL, 'extract') // Extract URLs | ||
* .$tokens($pattern.URL) // Extract URLs | ||
* .$filter(url => url.endsWith('.com')) | ||
@@ -105,0 +105,0 @@ */ |
@@ -15,3 +15,3 @@ /// <reference types="node" /> | ||
* `https://jsonplaceholder.typicode.com/todos/1` | ||
* .$fetch() // request url | ||
* .$http() // request url | ||
* .$json() // Convert from JSON | ||
@@ -18,0 +18,0 @@ */ |
@@ -15,3 +15,3 @@ import * as http from 'http'; | ||
* .$http() // Request URL | ||
* .$match($pattern.URL, 'extract') // Pull out URLs | ||
* .$tokens($pattern.URL) // Pull out URLs | ||
*/ | ||
@@ -31,3 +31,3 @@ $http(this: AsyncIterable<string>, opts?: Omit<HttpOpts, 'mode'>): $AsyncIterable<string>; | ||
* .$http('https://data-store.biz') // Post data | ||
* .$match($pattern.URL, 'extract') // Pull out URLs | ||
* .$tokens($pattern.URL) // Pull out URLs | ||
*/ | ||
@@ -34,0 +34,0 @@ $http(this: AsyncIterable<string | Buffer>, url: URL | string, opts?: Omit<HttpOpts, 'mode'>): $AsyncIterable<string>; |
@@ -40,19 +40,20 @@ import { $AsyncIterable, Pattern } from '../types'; | ||
/** | ||
* This operator allows for producing a single sequence of tokens out of lines of text. The default separator is whitespace. | ||
* This operator allows for producing a single sequence of tokens out of lines of text. The default token is all sequences of non-whitespace. | ||
* | ||
* @example | ||
* '<file>' | ||
* .$read() // Read file as lines | ||
* .$tokens(/\b[A-Za-z]{6,100}\b/i) // Extract 6+ letter words | ||
* | ||
* @example | ||
* '<file>' | ||
* .$read() // Read file as lines | ||
* .$tokens() // Convert to words | ||
* .$filter(x => x.length > 5) // Retain only words 6-chars or longer | ||
* .$tokens($pattern.URL) // Extract all URLs | ||
*/ | ||
$tokens(this: AsyncIterable<string>, sep?: Pattern): $AsyncIterable<string>; | ||
$tokens(this: AsyncIterable<string>, token?: Pattern): $AsyncIterable<string>; | ||
/** | ||
* `$match` is similar to tokens, but will emit based on a pattern instead of | ||
* just word boundaries. | ||
* `$match` provides the ability to easily retain or exclude lines. | ||
* | ||
* Additionally, mode will determine what is emitted when a match is found (within a single line): | ||
* * `undefined` - (default) Return entire line | ||
* * `'extract'` - Return only matched element | ||
* * `'negate'` - Return only lines that do not match | ||
@@ -69,6 +70,6 @@ * | ||
* .$read() | ||
* .$match(/\d{3}(-)?\d{3}(-)?\d{4}/, 'extract) | ||
* // Return all phone numbers in the sequence | ||
* .$match(/\d{3}(-)?\d{3}(-)?\d{4}/) | ||
* // Match all lines with phone numbers | ||
*/ | ||
$match(this: AsyncIterable<string>, regex: Pattern, mode?: 'extract' | 'negate'): $AsyncIterable<string>; | ||
$match(this: AsyncIterable<string>, regex: Pattern, mode?: 'negate'): $AsyncIterable<string>; | ||
/** | ||
@@ -75,0 +76,0 @@ * `$replace` behaves identically to `String.prototype.replace`, but will only operate |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const text_1=require("../util/text");function isIter(a){return!!a[Symbol.iterator]}class TextOperators{async*$columns(a){var b;let c=!a||"string"==typeof a||a instanceof RegExp?{sep:a}:Array.isArray(a)?{names:a}:a;const d=text_1.TextUtil.createRegExp((b=c.sep,null!==b&&void 0!==b?b:/\s+/),"");for await(const b of this){const a=b.split(d),{names:e}=c;if(e){const b=Math.min(a.length,e.length),c={};for(let d=0;d<b;d++)c[e[d]]=a[d];yield c}else yield a}}async*$tokens(a=/\s+/){a=text_1.TextUtil.createRegExp(a,"");for await(const b of this)yield*b.split(a)}async*$match(a,b){a=text_1.TextUtil.createRegExp(a,"extract"===b?"g":"");for await(const c of this)if("extract"===b){const b=[];c.replace(a,a=>b.push(a)?"":"");for(const a of b)yield a}else"negate"===b?a.test(c)||(yield c):a.test(c)&&(yield c)}$replace(a,b){if("string"==typeof a||a instanceof RegExp||isIter(a)){const c=text_1.TextUtil.createRegExp(a,"g");return this.$map(a=>a.replace(c,b))}else{const b=text_1.TextUtil.createRegExp(Object.keys(a));return this.$map(c=>c.replace(b,b=>a[b]))}}$trim(){return this.$map(a=>a.trim())}$toString(){return this.$collect().$map(a=>a.join(""))}}exports.TextOperators=TextOperators; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const text_1=require("../util/text");function isIter(a){return!!a[Symbol.iterator]}class TextOperators{async*$columns(a){var b;let c=!a||"string"==typeof a||a instanceof RegExp?{sep:a}:Array.isArray(a)?{names:a}:a;const d=text_1.TextUtil.createRegExp((b=c.sep,null!==b&&void 0!==b?b:/\s+/),"");for await(const b of this){const a=b.split(d),{names:e}=c;if(e){const b=Math.min(a.length,e.length),c={};for(let d=0;d<b;d++)c[e[d]]=a[d];yield c}else yield a}}async*$tokens(a=/\S+/){a=text_1.TextUtil.createRegExp(a,"g");for await(const b of this){const c=[];b.replace(a,a=>c.push(a)?"":""),yield*c}}async*$match(a,b){a=text_1.TextUtil.createRegExp(a,"");for await(const c of this)"negate"===b?a.test(c)||(yield c):a.test(c)&&(yield c)}$replace(a,b){if("string"==typeof a||a instanceof RegExp||isIter(a)){const c=text_1.TextUtil.createRegExp(a,"g");return this.$map(a=>a.replace(c,b))}else{const b=text_1.TextUtil.createRegExp(Object.keys(a));return this.$map(c=>c.replace(b,b=>a[b]))}}$trim(){return this.$map(a=>a.trim())}$toString(){return this.$collect().$map(a=>a.join(""))}}exports.TextOperators=TextOperators; |
{ | ||
"name": "@arcsine/nodesh", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "A node-based library, providing Unix shell-like functionality", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -16,4 +16,4 @@ <h1> | ||
$stdin // Automatically pipe from stdin | ||
.$match($pattern.URL, 'extract') // Retain only URL patterns and emit as single values | ||
.$fetch() // Request each url that comes through | ||
.$tokens($pattern.URL) // Retain only URL patterns and emit as single values | ||
.$http() // Request each url that comes through | ||
.$tokens() // Break down returned webpage into tokens | ||
@@ -124,3 +124,3 @@ .$filter(x => | ||
.$read() | ||
.$match(x => x.includes('potato')) | ||
.$match('potato') | ||
``` | ||
@@ -272,3 +272,3 @@ | ||
.$read() // Read a file | ||
.$match($pattern.URL, 'extract') // Extract URLs | ||
.$tokens($pattern.URL) // Extract URLs | ||
.$filter(url => url.endsWith('.com')) | ||
@@ -764,14 +764,19 @@ | ||
This operator allows for producing a single sequence of tokens out of lines of text. The default separator is whitespace. | ||
This operator allows for producing a single sequence of tokens out of lines of text. The default token is all sequences of non-whitespace. | ||
```typescript | ||
$tokens(this: AsyncIterable<string>, sep?: Pattern): $AsyncIterable<string>; | ||
$tokens(this: AsyncIterable<string>, token?: Pattern): $AsyncIterable<string>; | ||
``` | ||
Example | ||
```javascript | ||
'<file>' | ||
.$read() // Read file as lines | ||
.$tokens(/\b[A-Za-z]{6,100}\b/i) // Extract 6+ letter words | ||
``` | ||
```javascript | ||
'<file>' | ||
.$read() // Read file as lines | ||
.$tokens() // Convert to words | ||
.$filter(x => x.length > 5) // Retain only words 6-chars or longer | ||
.$tokens($pattern.URL) // Extract all URLs | ||
@@ -782,12 +787,10 @@ ``` | ||
`$match` is similar to tokens, but will emit based on a pattern instead of | ||
just word boundaries. | ||
`$match` provides the ability to easily retain or exclude lines. | ||
Additionally, mode will determine what is emitted when a match is found (within a single line): | ||
* `undefined` - (default) Return entire line | ||
* `'extract'` - Return only matched element | ||
* `'negate'` - Return only lines that do not match | ||
```typescript | ||
$match(this: AsyncIterable<string>, regex: Pattern, mode?: 'extract' | 'negate'): $AsyncIterable<string>; | ||
$match(this: AsyncIterable<string>, regex: Pattern, mode?: 'negate'): $AsyncIterable<string>; | ||
``` | ||
@@ -806,4 +809,4 @@ Example | ||
.$read() | ||
.$match(/\d{3}(-)?\d{3}(-)?\d{4}/, 'extract) | ||
// Return all phone numbers in the sequence | ||
.$match(/\d{3}(-)?\d{3}(-)?\d{4}/) | ||
// Match all lines with phone numbers | ||
@@ -810,0 +813,0 @@ ``` |
@@ -16,4 +16,4 @@ <h1> | ||
$stdin // Automatically pipe from stdin | ||
.$match($pattern.URL, 'extract') // Retain only URL patterns and emit as single values | ||
.$fetch() // Request each url that comes through | ||
.$tokens($pattern.URL) // Retain only URL patterns and emit as single values | ||
.$http() // Request each url that comes through | ||
.$tokens() // Break down returned webpage into tokens | ||
@@ -124,3 +124,3 @@ .$filter(x => | ||
.$read() | ||
.$match(x => x.includes('potato')) | ||
.$match('potato') | ||
``` | ||
@@ -127,0 +127,0 @@ |
106139
54
1189
1171