0.0.8 • Published 4 years ago

@zcorky/words-parser v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

words-parser

NPM version NPM quality Coverage Status Dependencies Build Status Known Vulnerabilities NPM download license issues

Parse and transform word in text, use custom renderers for words.

  • :sparkles: Support Chinese
  • 🌐 Support English (WIP)

Install

$ npm install @zcorky/words-parser

Usage

import * as parser from '@zcorky/words-parser';

type Args = {
  role: string;
};

const text: string = 'I am zero, who are you ?';
const words: parser.Words<Args> = [
  { text: 'zero', args: { role: 'hero' } },
  { text: 'you', args: { role: 'goodman' } },
];

const parsedToken = parser.parse(text, words);
// => [
//      { text: 'zero', args: { role: 'hero' }, start: 5, end: 9 },
//      { text: 'you', args: { role: 'goodman' }, start: 19, end: 22 },
//    ]

const transformedToken = parser.transform(text, parsedToken);
// => [
//      { type: 'TEXT', text: 'I\' am ', start: 0, end: 5 },
//      { type: 'CUSTOM', text: 'zero', args: { role: 'hero' }, start: 5, end: 9 },
//      { type: 'TEXT', text: ', who are ', start: 9, end: 19 },
//      { type: 'CUSTOM', text: 'you', args: { role: 'goodman' }, start: 19, end: 22 },
//      { type: 'TEXT', text: ', ' ?', start: 22, end: 24 },
//    ]

// Custom Renderer with React
const renderers: parser.Renderers<Args, ReactNode> = {
  TEXT(word) {
    return <span>{word.text}</span>;
  },
  CUSTOM(word) {
    return <Keyword text={word.text} />;
  },
};

API

License

MIT © Moeover

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago