text transform query
npm install tequery> text transform query
``bash`
$ npm install tequery
Basic text transform query.
$ is text body variable.
`js
import { tequery as tq } from 'tequery'
tq(' hoge ', '$.trim()').result
// => 'hoge'
// equaliy shorthand
tq(' hoge ', '.trim()').result
tq('https://example.com', .replace('https://', '')).result`
// => 'example.com'
`js line1
const text =
line2
line3
tq(text, '$$.trim()').result
// => 'line1\n'
// + 'line2\n'
// + 'line3'
`
final process to string from returned value type.
| type | process |
| ------------------------ | ---------------- |
| typeof v === 'function' | v($) |v
| typeof v === 'string' | |String(v)
| typeof v === 'number' | |""
| typeof v === 'undefined' | |""
| v === null | |v.join("\n")
| Array.isArray(v) | |String(v)
| other | |JSON.stringify
| typeof v === 'object' | |
util functions in running scope.
`jslen('abc')
tq('', ).result
// => '3'
tq('abcde', 'len($)').result
// => '5'
// equaliy shorthand
tq('abcde', 'len').result
`
| func | usage | return |
| --------------- | --------------------------- | ------------- |
| len | len('aaa') | 3 |lineNum
| , ln | ln('a\nb') | 2 |count
| | count('aaa-a-', 'a') | 4 |pack
| | pack('a\nb\n\nc\n\n\n') | 'a\nb\nc\n' |shiftl
| | shiftl('a-b-c-d', '-', 1) | 'b-c-d' |shiftr
| | shiftr('a-b-c-d', '-', 1) | 'a-b-c' |
util vars in running scope.
`js$csv.join(':')
tq('a,b,c', ).result$tsv.join(':')
// => 'a:b:c'
expect(tq('', ).result).toBe()$sp('+').join(':')
expect(tq('a+b+c', ).result).toBe('a:b:c')`
| vars | $= | is |
| --------------- | --------- | ----------------- |
| $tsv | a\tb\tc | ['a', 'b', 'c'] |$csv
| | a,b,c | ['a', 'b', 'c'] |$ls
| , $lines | a\nb\nc | ['a', 'b', 'c'] |$sp('+')
| | a+b+c | ['a', 'b', 'c'] |
#### more example
vars with line-run
`tsa,b,c
const txt =
d,e,f
tq(text, $$csv.join(':')).result`
// => 'a:b:c\n'
// + 'd:e:f'
`ts``
type Result = {
status: 'ok' | 'ng' // is changed
result: string
resultRaw: unknown
evalQuery: string // compiled query
errorText: string
returnType: string
comps: Complements // is shorthund enabled
}