Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including strind with all npm packages installed. Try it out:

var strind = require("strind")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

strind v0.3.1

Partition strings based on character indices

strind

NPM Build Coverage

Partition strings based on character indices.

Install

yarn add strind

Usage

import strind from 'strind';

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

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

Callback

An optional callback function can be passed 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';

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

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

Changelog

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free