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

Julien Puydt julien.puydt at laposte.net
Thu Jul 6 16:53:26 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 297cc8885c0e35eb16f8eae70e861b52a02dfc45
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Thu Jul 6 18:41:21 2017 +0200

    New upstream version 2.0.1
---
 .gitignore       |  3 +++
 CHANGELOG.md     | 14 +++++++++++
 README.md        | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json     | 43 ++++++++++++++++++++++++++++++++
 rollup.config.js | 15 +++++++++++
 src/index.ts     | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test.js     | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tsconfig.json    | 16 ++++++++++++
 types.d.ts       | 17 +++++++++++++
 9 files changed, 325 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8d67a86
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+dist
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/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"
+}
diff --git a/rollup.config.js b/rollup.config.js
new file mode 100644
index 0000000..85c70df
--- /dev/null
+++ b/rollup.config.js
@@ -0,0 +1,15 @@
+import typescript from 'rollup-plugin-typescript';
+
+export default {
+	entry: 'src/index.ts',
+	moduleName: 'locateCharacter',
+	plugins: [
+		typescript({
+			typescript: require('typescript')
+		})
+	],
+	targets: [
+		{ dest: 'dist/locate-character.es.js', format: 'es' },
+		{ dest: 'dist/locate-character.umd.js', format: 'umd' }
+	]
+};
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..ed810bc
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,76 @@
+interface Options {
+	offsetLine?: number
+	offsetColumn?: number
+	startIndex?: number
+}
+
+interface Range {
+	start: number
+	end: number
+	line: number
+}
+
+interface Location {
+	line: number
+	column: number
+	character: number
+}
+
+export function getLocator ( source: string, options: Options = {} ) {
+	const offsetLine = options.offsetLine || 0;
+	const offsetColumn = options.offsetColumn || 0;
+
+	let originalLines = source.split( '\n' );
+
+	let start = 0;
+	let lineRanges = originalLines.map( ( line, i ) => {
+		const end = start + line.length + 1;
+		const range: Range = { start, end, line: i };
+
+		start = end;
+		return range;
+	});
+
+	let i = 0;
+
+	function rangeContains ( range: Range, index: number ) {
+		return range.start <= index && index < range.end;
+	}
+
+	function getLocation ( range: Range, index: number ): Location {
+		return { line: offsetLine + range.line, column: offsetColumn + index - range.start, character: index };
+	}
+
+	function locate ( search: string, startIndex?: number ): Location
+	function locate ( search: number ): Location
+	function locate ( search: any, startIndex?: number ): Location {
+		if ( typeof search === 'string' ) {
+			search = source.indexOf( search, startIndex || 0 );
+		}
+
+		let range = lineRanges[i];
+
+		const 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: string, search: string, options?: Options ): Location
+function locate ( source: string, search: number, options?: Options ): Location
+function locate ( source: string, search: any, options?: Options ): Location {
+	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 { locate };
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..5af98a9
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,73 @@
+const fs = require( 'fs' );
+const path = require( 'path' );
+const assert = require( 'assert' );
+const { locate, getLocator } = require( '..' );
+
+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();
+
+describe( 'locate-character', () => {
+	describe( 'getLocator', () => {
+		const locator = getLocator( sample );
+
+		it( 'returns a function', () => {
+			assert.equal( typeof getLocator( sample ), 'function' );
+		});
+
+		it( 'locates by character index', () => {
+			assert.deepEqual( locator( 0 ), { line: 0, column: 0, character: 0 });
+			assert.deepEqual( locator( 1 ), { line: 0, column: 1, character: 1 });
+			assert.deepEqual( locator( 76 ), { line: 2, column: 9, character: 76 });
+		});
+
+		it( 'respects offsetLine: x and offsetColumn: x', () => {
+			const locator = getLocator( sample, { offsetLine: 2, offsetColumn: 2 });
+			assert.deepEqual(
+				locator( 0 ),
+				{ line: 2, column: 2, character: 0 }
+			);
+		});
+
+		it( 'locates by search string', () => {
+			assert.deepEqual( locator( 'fly' ), { line: 0, column: 13, character: 13 });
+		});
+
+		it( 'locates by search string with startIndex', () => {
+			let location = locator( 'fly' );
+			assert.deepEqual( location, { line: 0, column: 13, character: 13 });
+
+			location = locator( 'fly', location.character + 1 );
+			assert.deepEqual( location, { line: 2, column: 9, character: 76 });
+
+			location = locator( 'fly', location.character + 1 );
+			assert.deepEqual( location, { line: 3, column: 8, character: 104 });
+		});
+	});
+
+	describe( 'locate', () => {
+		it( 'locates a character by index', () => {
+			assert.deepEqual( locate( sample, 13 ), { line: 0, column: 13, character: 13 });
+		});
+
+		it( 'locates a character by string', () => {
+			assert.deepEqual( locate( sample, 'fly' ), { line: 0, column: 13, character: 13 });
+		});
+
+		it( 'locates a character by string with startIndex', () => {
+			assert.deepEqual( locate( sample, 'fly', { startIndex: 14 }), { line: 2, column: 9, character: 76 });
+			assert.deepEqual( locate( sample, 'fly', { startIndex: 77 }), { line: 3, column: 8, character: 104 });
+		});
+
+		it( 'respects offsetLine: x and offsetColumn: x', () => {
+			assert.deepEqual(
+				locate( sample, 13, { offsetLine: 2, offsetColumn: 2 }),
+				{ line: 2, column: 15, character: 13 }
+			);
+		});
+	});
+});
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..e07bff8
--- /dev/null
+++ b/tsconfig.json
@@ -0,0 +1,16 @@
+{
+    "compilerOptions": {
+        "noImplicitAny": true,
+        "diagnostics": true,
+        "noImplicitThis": true,
+        "noEmitOnError": true,
+        "lib": ["es5", "es6"]
+    },
+    "target": "ES5",
+    "include": [
+        "src"
+    ],
+    "exclude": [
+        "node_modules"
+    ]
+}
\ No newline at end of file
diff --git a/types.d.ts b/types.d.ts
new file mode 100644
index 0000000..ff8c0b1
--- /dev/null
+++ b/types.d.ts
@@ -0,0 +1,17 @@
+interface Options {
+	offsetLine?: number
+	offsetColumn?: number
+	startIndex?: number
+}
+
+export interface Location {
+	line: number
+	column: number
+	character: number
+}
+
+export function locate ( source: string, search: string, options?: Options ): Location
+export function locate ( source: string, search: number, options?: Options ): Location
+
+export function getLocator ( source: string, options?: Options ): ( search: string, startIndex?: number ) => Location
+export function getLocator ( source: string, options?: Options ): ( search: number ) => Location
\ No newline at end of file

-- 
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