text-part
v1.1.4
Published
Parse blobs of text into sections, lines, and identifiers.
Downloads
2
Maintainers
Readme
TextPart
Synopsis
Transform blobs of text:
One
Two
Three Elephants walking.
Four tigers sleeping.
Into array structures using RegExp
identifiers:
[
{
"type": "section",
"title": "Example"
"lines": [
[ "One" ], [ "Two" ],
[ "Three ", { "type": "identifier", text: "Elephants", extra: "data"}, " walking."],
[ "Four tigers sleeping." ]
],
"extra": "data"
}
]
Usage:
- Install:
npm install text-part --save
- Example: example.js
Kind: global class
new TextPart(name, sections, identifiers, config)
| Param | Type | Description | | --- | --- | --- | | name | string | The name to describe all text types which are processed by the TextPart instance. | | sections | Array.<(string|object)> | An array of section identifiers. (RegExp/string) | | identifiers | Array.<(string|object)> | An array of identifiers. (RegExp/string) | | config | Object | Configuration options to define how TextPart transforms. |
textPart.transform(text) ⇒ Array.<(object|Array.<(string|object)>)>
Transform a file data string into an array of parts. The top level array represents the lines. The arrays, called parts, within each line represent parts within the line. Some parts are simple strings, others represent things such as identifiers.
Kind: instance method of TextPart
| Param | Type | Description | | --- | --- | --- | | text | string | The text to transform into parts. |
textPart.clearRules()
Clear all added identifiers.
Kind: instance method of TextPart
textPart.loadSections(sections)
Load an array of section identifiers.
Kind: instance method of TextPart
| Param | Type | Description | | --- | --- | --- | | sections | Array.<(RegExp|string)> | An array of strings or RegExp to use as identifiers. |
textPart.loadIdentifiers(identifiers)
Load an array of identifiers.
Kind: instance method of TextPart
| Param | Type | Description | | --- | --- | --- | | identifiers | Array.<(string|object)> | An array of objects or strings to be loaded as identifiers. [ 'identifier1', { regex: 'identifier2', data: {} }, { regex: ['identifier3','identifier4'], data: {} } ] |
textPart.addSectionIdentifier(identifier, data, config)
Add a section identifier. This is regex which will split the text blob when found.
Kind: instance method of TextPart
| Param | Type | Description | | --- | --- | --- | | identifier | RegExp | String | The RegExp to use for identification. | | data | object | Data to associate with the identifier and anything the identifier creates. | | config | object | The config options to use. |
textPart.addIdentifier(identifier, data, config)
Kind: instance method of TextPart
| Param | Type | Description | | --- | --- | --- | | identifier | RegExp | String | The RegExp to use for identification. | | data | object | Data to associate with the identifier and anything the identifier creates. | | config | object | The config options to use. |