Package Exports
- string-match-left-right
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-match-left-right) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
string-match-left-right
Do substrings match what's on the left or right of the given index?
Table of Contents
- Install
- The API
opts.cbLeft
andopts.cbRight
- Regarding the unit test code coverage
- Contributing
- Licence
Install
$ npm i string-match-left-right
// CommonJS way:
const { matchLeftIncl, matchRightIncl, matchLeft, matchRight } = require('string-match-left-right')
// ES Modules way:
import { matchLeftIncl, matchRightIncl, matchLeft, matchRight } from 'string-match-left-right'
Here's what you'll get:
Type | Key in package.json |
Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled, contains require and module.exports |
main |
dist/string-match-left-right.cjs.js |
3 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . |
module |
dist/string-match-left-right.esm.js |
3 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in |
browser |
dist/string-match-left-right.umd.js |
17 KB |
The API
There are four methods; all have the same API's:
matchLeftIncl
— at least one of given substrings has to match what's on the left and including character at a given indexmatchRightIncl
— at least one of given substrings has to match what's on the right and including character at a given indexmatchLeft
— at least one of given substrings has to match what's on the left of the given indexmatchRight
— at least one of given substrings has to match what's on the right of the given index
Input argument | Type | Obligatory? | Description |
---|---|---|---|
str |
String | yes | Source string to work on |
position |
Natural number incl. zero | yes | Starting index. Can be zero. Otherwise, a natural number. |
whatToMatch |
String or array of strings | yes | What should we look for on the particular side, left or right. If array is given, at one or more matches will yield in result true |
opts |
Plain object | no | Optional options object. See below. |
Optional Options Object's API:
options object's key |
Type | Obligatory? | Default | Description |
---|---|---|---|---|
{ | ||||
i |
Boolean | no | false |
if false , it's case sensitive. If true , it's insensitive. |
cbLeft |
Function | no | undefined |
if you supply a callback function as a value of this key, it will be fed with the character that right outside on the left of substring being checked. |
cbRight |
Function | no | undefined |
if you supply a callback function as a value of this key, it will be fed with the character that right outside on the left of substring being checked. |
} |
Options' defaults:
{
i: false
}
Options object is sanitized by check-types-mini which will throw
if you set options' keys to wrong types or add unrecognized keys.
// K E Y
// -----
// test string with character indexes to help you count:
//
// test string: abcdefghi
// indexes: 012345678
//
// that is, c is number (term "number" further abbreviated as hash character "#") 2 or i is #8.
//
// we'll be using the same string "abcdefghi" below:
const { matchLeftIncl, matchRightIncl, matchLeft, matchRight } = require('string-match-left-right')
let res1 = matchLeftIncl('abcdefghi', 3, ['bcd']),
// 3rd character is "d" because indexes start from zero.
// We're checking the string to the left of it, "bcd", inclusive of current character ("d").
// This means, "bcd" has to end with existing character and the other chars to the left
// must match exactly:
console.log(`res1 = ${res1}`)
// => res1 = true
let res2 = matchLeft('abcdefghi', 3, ['ab', `zz`]),
// neither "ab" nor "zz" are to the left of 3rd index, "d":
console.log(`res2 = ${res2}`)
// => res2 = false
let res3 = matchRightIncl('abcdefghi', 3, ['def', `zzz`]),
// "def" is to the right of 3rd index (including it), "d":
console.log(`res3 = ${res3}`)
// => res3 = true
let res4 = matchRight('abcdefghi', 3, ['ef', `zz`]),
// One of values, "ef" is exactly to the right of 3rd index, "d":
console.log(`res4 = ${res4}`)
// => res4 = true
opts.cbLeft
and opts.cbRight
Often you need not only to match what's on the left/right of the given index within string, but also to perform checks on what's outside.
For example, if you are traversing the string and want to match the class
attribute, you traverse backwards, "catch" equals character =
, then check, what's on the left of it using method matchLeft
. That's not enough, because you also need to check, is the character further to the left of it is a space, or in algorithm terms, "trims to length zero", that is (trim(char).length === 0)
. How do you apply this check?
Using opts.cbLeft
callback ("cb" in it's name stands for CallBack):
const { matchLeftIncl, matchRightIncl, matchLeft, matchRight } = require('string-match-left-right')
// imagine you looped the string and wanted to catch where does attribute "class" start
// and end (not to mention to ensure that it's a real attribute, not something ending with this
// string "class").
// You catch "=", index number 8.
// This library can check, is "class" to the left of it and feed what's to the left of it
// to your supplied callback function, which happens to be a checker "is it a space":
function isSpace(char) {
return (typeof char === 'string') && (char.trim() === '')
}
let res = matchLeft('<a class="something">', 8, 'class', { cbLeft: isSpace }),
console.log(`res = ${JSON.stringify(res, null, 4)}`)
// => res = true
Regarding the unit test code coverage
You may ask: why is the coverage for proper 100%?
I will answer: it's because the source is in ES Modules (import
/export
) and because Node does not support ES modules yet, I have to transpile the code (using Rollup + Babel). This means, we run unit tests not against the source code, but against what Babel generated out of it. Since Babel adds more stuff and that stuff can change since we're using "floating" preset babel-preset-env
, I can't 100% guarantee that unit tests will cover transpiled code 100%.
However, at least we cover 100% of the lines:
Contributing
Hi! 99% of society are passive people, consumers. They wait for others to take action, they prefer to blend in. Rest 1% are proactive, vocal (usually also opinionated) citizens who will do something rather than wait, hoping others will do it eventually. If you are one of that 1 %, you're in luck because I am the same and together we can make something happen.
If you want a new feature in this package or you would like to change some of its functionality, raise an issue on this repo. Also, you can email me.
If you tried to use this library but it misbehaves, or you need an advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo. Alternatively, you can email me.
If you don't like the code in here and would like to advise how something could be done better, please do. Same drill - GitHub issues or email, your choice.
If you would like to add or change some features, just fork it, hack away, and file a pull request. I'll do my best to merge it quickly. Code style is
airbnb
, just without semicolons. If you use a good code editor, it will pick up the established ESLint setup.
Licence
MIT License (MIT)
Copyright © 2017 Codsen Ltd, Roy Revelt