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
$ 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 arguments of the same type.
matchLeftIncl() matchRightIncl() matchLeft() matchRight()
Each of four functions' API:
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. |
} |
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
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