{"_id":"txt","_rev":"4-51ad9864ab550b685af1c9a2cb97bea3","name":"txt","description":"Utilities for working with text.","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"txt","version":"1.0.0","description":"Utilities for working with text.","main":"txt.js","scripts":{"test":"gulp"},"keywords":["text","string","match","delimit","truncate","escape","pluralize","capitalize"],"repository":{"type":"git","url":"https://github.com/wyvrw/txt.git"},"author":{"name":"wyvrw"},"license":"MIT","bugs":{"url":"https://github.com/wyvrw/txt/issues"},"homepage":"https://github.com/wyvrw/txt","devDependencies":{"chai":"^1.9.1","gulp":"^3.8.7","gulp-mocha":"^1.0.0","gulp-rename":"^1.2.0","gulp-uglify":"^0.3.1","mocha":"^1.21.4"},"_id":"txt@1.0.0","_shasum":"818ece909bd35f29a845596cefb06f95502ca01d","_from":".","_npmVersion":"1.4.7","_npmUser":{"name":"wyvrw","email":"awyvrw@gmail.com"},"maintainers":[{"name":"wyvrw","email":"awyvrw@gmail.com"}],"dist":{"shasum":"818ece909bd35f29a845596cefb06f95502ca01d","tarball":"https://registry.npmjs.org/txt/-/txt-1.0.0.tgz","integrity":"sha512-9RD5J68YdZzkXQvu/IfQ/19+bRCvejxAoFlFkM079HFoEBpE+RAcd+3D+MmcINSg2+KBilWOLZ1uh7MqKaCyZg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHhv3+gMGWjhTNSSsX55ZCNWmePYwNSkmUPZ0H1qNK3hAiBuMBKkfMnz+bNgTT6d0yakXXHwOp9khbtOe71A4j6uTw=="}]}}},"readme":"# txt\n\nA lightweight javascript library with versatile functions for working with text.\n\n1 KB (minified and gzipped) and dependency free, txt.js features advanced matching and delimiting, smart truncation, HTML and RegExp escaping, simple pluralization, and more.\n\nIt runs in both the browser and Node.js.\n\n## contents\n- [Start](#start)\n- [Documentation](#documentation)\n- [License](#license)\n\n## start\n\n### browser\n\nDo one of the following to download txt:\n\n- Use [Bower](http://bower.io): `bower install txt --save`\n- Use [npm](https://www.npmjs.org): `npm install txt --save`\n- Clone the repo: `git clone https://github.com/wyvrw/txt.git`\n- [Download the repo as a zip](https://github.com/wyvrw/txt/archive/master.zip)\n\nThen in your HTML add:\n\n```html\n<script src=\"txt.js\"></script>\n```\n\n### node.js\n\nUse npm to download txt: `npm install txt --save`\n\nThen in your javascript add:\n\n```javascript\nvar txt = require('txt');\n```\n\n## documentation\n\n### `eachMatch()`\n\nCalls a function for each match in the text, giving detailed information about it. If the function returns a string then that match is replaced. Returns the modified text.\n\n```javascript\ntxt.eachMatch('You can learn about #js or #html.', '#', function(match, details) {\n\treturn '';\n});\n// 'You can learn about js or html.'\n\ntxt.eachMatch('You can learn about #js or #html.', /#\\w+/g, function (match, details) {\n    return '<a href=\"' + match + '\" title=\"option ' + (details.arrayIndex + 1) + ' of ' + details.array.length + '\">' + match + '</a>';\n});\n// 'You can learn about <a href=\"#js\" title=\"option 1 of 2\">#js</a> or <a href=\"#html\" title=\"option 2 of 2\">#html</a>.'\n```\n\nThe callback function should have 2 parameters:\n\n1. The match\n2. An object containing:\n\t- The `string` of the text\n\t- The `stringIndex` of the current match\n\t- The `array` of all the matches\n\t- The `arrayIndex` of the current match\n\t- The `pattern` used for the matches\n\nPerformance tip: Use a RegExp with the global flag as the pattern - strings have to be escaped and then converted into RegExps and RegExps without the global flag have to be created again.\n\n### `eachDelimiter()`\n\nCalls a function for each delimiter in the text, giving detailed information about it. If the function returns a string then that match is replaced. Returns the modified text.\n\n```javascript\ntxt.eachDelimiter('An example...\\n...is an example.', '\\n', function (match, details) {\n\treturn 'Line ' + (details.arrayIndex + 1) + ': ' + match;\n});\n// 'Line 1: An example...Line 2: ...is an example.'\n\ntxt.eachDelimiter('An example...\\n...is an example.', /\\n/g, function (match, details) {\n\treturn match + ' (' + (details.stringIndex + match.length) + ' characters so far)';\n});\n// 'An example... (13 characters so far)\\n...is an example. (54 characters so far)'\n```\n\nThe callback function should have 2 parameters:\n\n1. The match\n2. An object containing:\n\t- The `string` of the text\n\t- The `stringIndex` of the current match\n\t- The `array` of all the matches\n\t- The `arrayIndex` of the current match\n\t- The `delimiter` used for the matches\n\nPerformance tip: When using a RegExp as the delimiter, make sure to set the global flag - RegExps without the global flag have to be created again.\n\n### `truncateChars()`\n\nReturns text truncated to the nearest delimiter within a specified number of characters.\n\n```javascript\ntxt.truncateChars('A bat, a cat, a dog, and a rat.', 25);\n// 'A bat, a cat, a dog, and a ...'\n\ntxt.truncateChars('A bat, a cat, a dog, and a rat.', 25, {\n\tdelimiter: ',', // default ' '\n\tappend: ' and others.' // default '...'\n});\n// 'A bat, a cat, and others.'\n```\n\n### `truncateItems()`\n\nReturns the text truncated to the specified number of items according to a delimiter.\n\n```javascript\ntxt.truncateItems('A bat, a cat, a dog, and a rat.', 3);\n// 'A bat, a ...'\n\ntxt.truncateItems('A bat, a cat, a dog, and a rat.', 3, {\n\tdelimiter: ',', // default ' '\n\tappend: ' and others.' // default '...'\n});\n// 'A bat, a cat, a dog, and others.'\n```\n\n### `escapeHTML()` & `unescapeHTML()`\n\nReturns escaped/unescaped HTML.\n\n```javascript\ntxt.escapeHTML('An example...<br>...is an example.');\n// 'An example...&lt;br&gt;...is an example.\n\ntxt.unescapeHTML('An example...&lt;br&gt;...is an example.');\n// 'An example...<br>...is an example.'\n```\n\n### `escapeRegExp()` & `unescapeRegExp()`\n\nReturns escaped/unescaped Regular Expression.\n\n```javascript\ntxt.escapeRegExp('[123]');\n// '\\[123\\]'\n\ntxt.unescapeRegExp('\\[123\\]');\n// '[123]'\n```\n\n### `pluralize()`\n\nReturns the singular or plural version of a word, depending on a number.\n\n```javascript\ntxt.pluralize(0, {\n\tsingular: 'person',\n\tplural: 'people'\n});\n// 'people'\n\ntxt.pluralize(1, {\n\tsingular: 'person',\n\tplural: 'people'\n});\n// 'person'\n```\n\n### `capitalize()`\n\nReturns text with the first character capitalized.\n\n```javascript\ntxt.capitalize('capital');\n// 'Capital'\n```\n\n### `defaults()`\n\nOverides the defaults. Returns the new defaults.\n\n```javascript\ntxt.defaults({ ... });\n// { ... }\n```\n\nThe defaults are:\n\n```javascript\n{\n\ttruncateChars: { append: '...', delimiter: ' ' },\n\ttruncateItems: { append: '...', delimiter: ' ' }\n}\n```\n\n### `version`\n\nThe version of txt.js.\n\n```javascript\ntxt.version;\n// '1.0.0'\n```\n\n## license\n\ntxt.js is released under the [MIT license](LICENSE).\n","maintainers":[{"name":"wyvrw","email":"awyvrw@gmail.com"}],"time":{"modified":"2022-06-28T00:34:58.140Z","created":"2014-08-26T00:57:18.577Z","1.0.0":"2014-08-26T00:57:18.577Z"},"homepage":"https://github.com/wyvrw/txt","keywords":["text","string","match","delimit","truncate","escape","pluralize","capitalize"],"repository":{"type":"git","url":"https://github.com/wyvrw/txt.git"},"author":{"name":"wyvrw"},"bugs":{"url":"https://github.com/wyvrw/txt/issues"},"license":"MIT","readmeFilename":"README.md"}