Skip to content
/ strind Public

Partition strings based on character indices

License

Notifications You must be signed in to change notification settings

metonym/strind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

strind

NPM Build

Partition strings based on character indices.

Install

yarn add strind

Usage

Required arguments are the string and an array of tuples that denote the start and end parse indices.

import strind from "strind";

const result = strind("abcd", [
  [1, 1],
  [2, 6],
]);

console.log(result);
/**
 * {
      matched: ['b', 'cd'],
      unmatched: [
        {
          chars: 'a',
          index: 0
        }
      ]
    }
 *
 */

Callback

The module accepts an optional callback as the third argument.

The function is called with the substring chars and boolean matches if the substring matches the array indices.

import strind from "strind";

// signature
// strind(string, Array<Tuple>, [function])

const result = strind(
  "abcd",
  [
    [1, 1],
    [2, 6],
  ],
  ({ chars, matches }) => ({
    isHighlighted: matches,
    text: chars,
  })
);

console.log(result);
/**
 * [
      { isHighlighted: false, text: 'a' },
      { isHighlighted: true, text: 'b' },
      { isHighlighted: true, text: 'cd' }
    ]
 *
 */

License

MIT

About

Partition strings based on character indices

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •