[Pkg-javascript-commits] [node-locate-character] 01/02: New upstream version 2.0.1

Julien Puydt julien.puydt at laposte.net
Mon Jul 3 17:48:15 UTC 2017


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-locate-character.

commit 983ccce95955769e7d3ff00c5aebc7a678f3bff8
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Mon Jul 3 19:33:47 2017 +0200

    New upstream version 2.0.1
---
 CHANGELOG.md                 | 14 +++++++++
 README.md                    | 68 ++++++++++++++++++++++++++++++++++++++++++++
 dist/locate-character.es.js  | 43 ++++++++++++++++++++++++++++
 dist/locate-character.umd.js | 54 +++++++++++++++++++++++++++++++++++
 package.json                 | 43 ++++++++++++++++++++++++++++
 5 files changed, 222 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..5cdf38e
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,14 @@
+# locate-character changelog
+
+## 2.0.1
+
+* Port to TypeScript, add typings (no change in functionality)
+
+## 2.0.0
+
+* BREAKING: `startIndex` must be passed as a property of `options`, not as a standalone argument
+* Support `offsetLine` and `offsetColumn`
+
+## 1.0.0
+
+* First release
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3cbc35f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,68 @@
+# locate-character
+
+Get the line and column number of a particular character in a string.
+
+## Installation
+
+`npm install locate-character`, or get it from [unpkg.com/locate-character](https://unpkg.com/locate-character).
+
+
+## Usage
+
+To search for a particular character, using the index or a search string, use `locate`:
+
+```js
+import { locate } from 'locate-character';
+
+const sample = `
+A flea and a fly in a flue
+Were imprisoned, so what could they do?
+Said the fly, "let us flee!"
+"Let us fly!" said the flea.
+So they flew through a flaw in the flue.
+`.trim();
+
+// Using a character index
+const index = sample.indexOf( 'fly' );
+locate( sample, index );
+// -> { line: 0, column: 13, character: 13 }
+
+// Using the string itself
+locate( sample, 'fly' );
+// -> { line: 0, column: 13, character: 13 }
+
+// Using the string with a start index
+locate( sample, 'fly', { startIndex: 14 });
+// -> { line: 2, column: 9, character: 76 }
+```
+
+If you will be searching the same string repeatedly, it's much faster if you use `getLocator`:
+
+```js
+import { getLocator } from 'locate-character';
+
+const locate = getLocator( sample );
+
+let location = locate( 13 );
+// -> { line: 0, column: 13, character: 13 }
+
+location = locate( 'fly', { startIndex: location.character + 1 });
+// -> { line: 2, column: 9, character: 76 }
+
+location = locate( 'fly', { startIndex: location.character + 1 });
+// -> { line: 3, column: 8, character: 104 }
+```
+
+In some situations (for example, dealing with sourcemaps), you need one-based line numbers:
+
+```js
+getLocator( sample, { offsetLine: 1 });
+locate( sample, { offsetLine: 1 });
+```
+
+There's also an `offsetColumn` option which is less useful in real-world situations.
+
+
+## License
+
+MIT
diff --git a/dist/locate-character.es.js b/dist/locate-character.es.js
new file mode 100644
index 0000000..45b29c0
--- /dev/null
+++ b/dist/locate-character.es.js
@@ -0,0 +1,43 @@
+function getLocator(source, options) {
+    if (options === void 0) { options = {}; }
+    var offsetLine = options.offsetLine || 0;
+    var offsetColumn = options.offsetColumn || 0;
+    var originalLines = source.split('\n');
+    var start = 0;
+    var lineRanges = originalLines.map(function (line, i) {
+        var end = start + line.length + 1;
+        var range = { start: start, end: end, line: i };
+        start = end;
+        return range;
+    });
+    var i = 0;
+    function rangeContains(range, index) {
+        return range.start <= index && index < range.end;
+    }
+    function getLocation(range, index) {
+        return { line: offsetLine + range.line, column: offsetColumn + index - range.start, character: index };
+    }
+    function locate(search, startIndex) {
+        if (typeof search === 'string') {
+            search = source.indexOf(search, startIndex || 0);
+        }
+        var range = lineRanges[i];
+        var d = search >= range.end ? 1 : -1;
+        while (range) {
+            if (rangeContains(range, search))
+                return getLocation(range, search);
+            i += d;
+            range = lineRanges[i];
+        }
+    }
+    ;
+    return locate;
+}
+function locate(source, search, options) {
+    if (typeof options === 'number') {
+        throw new Error('locate takes a { startIndex, offsetLine, offsetColumn } object as the third argument');
+    }
+    return getLocator(source, options)(search, options && options.startIndex);
+}
+
+export { getLocator, locate };
\ No newline at end of file
diff --git a/dist/locate-character.umd.js b/dist/locate-character.umd.js
new file mode 100644
index 0000000..bd3258d
--- /dev/null
+++ b/dist/locate-character.umd.js
@@ -0,0 +1,54 @@
+(function (global, factory) {
+    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+    typeof define === 'function' && define.amd ? define(['exports'], factory) :
+    (factory((global.locateCharacter = global.locateCharacter || {})));
+}(this, (function (exports) { 'use strict';
+
+function getLocator(source, options) {
+    if (options === void 0) { options = {}; }
+    var offsetLine = options.offsetLine || 0;
+    var offsetColumn = options.offsetColumn || 0;
+    var originalLines = source.split('\n');
+    var start = 0;
+    var lineRanges = originalLines.map(function (line, i) {
+        var end = start + line.length + 1;
+        var range = { start: start, end: end, line: i };
+        start = end;
+        return range;
+    });
+    var i = 0;
+    function rangeContains(range, index) {
+        return range.start <= index && index < range.end;
+    }
+    function getLocation(range, index) {
+        return { line: offsetLine + range.line, column: offsetColumn + index - range.start, character: index };
+    }
+    function locate(search, startIndex) {
+        if (typeof search === 'string') {
+            search = source.indexOf(search, startIndex || 0);
+        }
+        var range = lineRanges[i];
+        var d = search >= range.end ? 1 : -1;
+        while (range) {
+            if (rangeContains(range, search))
+                return getLocation(range, search);
+            i += d;
+            range = lineRanges[i];
+        }
+    }
+    ;
+    return locate;
+}
+function locate(source, search, options) {
+    if (typeof options === 'number') {
+        throw new Error('locate takes a { startIndex, offsetLine, offsetColumn } object as the third argument');
+    }
+    return getLocator(source, options)(search, options && options.startIndex);
+}
+
+exports.getLocator = getLocator;
+exports.locate = locate;
+
+Object.defineProperty(exports, '__esModule', { value: true });
+
+})));
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..161901e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,43 @@
+{
+  "name": "locate-character",
+  "version": "2.0.1",
+  "description": "Get the line and column number of a specific character in a string",
+  "main": "dist/locate-character.umd.js",
+  "jsnext:main": "dist/locate-character.es.js",
+  "module": "dist/locate-character.es.js",
+  "scripts": {
+    "test": "mocha",
+    "pretest": "npm run build",
+    "build": "rollup -c",
+    "prepublish": "npm test"
+  },
+  "files": [
+    "dist",
+    "README.md"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "git+https://gitlab.com/Rich-Harris/locate-character.git"
+  },
+  "keywords": [
+    "string",
+    "character",
+    "locate",
+    "line",
+    "column",
+    "location"
+  ],
+  "author": "Rich Harris",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://gitlab.com/Rich-Harris/locate-character/issues"
+  },
+  "homepage": "https://gitlab.com/Rich-Harris/locate-character#README",
+  "devDependencies": {
+    "mocha": "^3.0.1",
+    "rollup": "^0.34.7",
+    "rollup-plugin-typescript": "^0.8.1",
+    "typescript": "^2.3.2"
+  },
+  "types": "./types.d.ts"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-locate-character.git



More information about the Pkg-javascript-commits mailing list