├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── .github └── workflows │ └── main.yml ├── index.test-d.ts ├── license ├── package.json ├── readme.md ├── index.js ├── index.d.ts └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 24 14 | - 20 15 | steps: 16 | - uses: actions/checkout@v5 17 | - uses: actions/setup-node@v6 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {parseLineColumnPath, stringifyLineColumnPath, type ParsedPath} from './index.js'; 3 | 4 | const parsed: ParsedPath = parseLineColumnPath('unicorn.js:8:14'); 5 | expectType(parsed); 6 | expectType(parseLineColumnPath({file: 'unicorn.js'})); 7 | expectType(parseLineColumnPath({file: 'unicorn.js', line: 1})); 8 | expectType(parseLineColumnPath({file: 'unicorn.js', column: 1})); 9 | expectType(parseLineColumnPath({file: new URL('file://path/to/unicorn.js')})); 10 | expectType(parseLineColumnPath(new URL('file://path/to/unicorn.js'))); 11 | 12 | expectType(stringifyLineColumnPath(parsed)); 13 | expectType(stringifyLineColumnPath({file: 'unicorn.js'})); 14 | expectType(stringifyLineColumnPath({file: 'unicorn.js', line: 1})); 15 | expectType(stringifyLineColumnPath({file: 'unicorn.js', column: 1})); 16 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "line-column-path", 3 | "version": "4.0.0", 4 | "description": "Parse and stringify file paths with line and column like `unicorn.js:8:14`", 5 | "license": "MIT", 6 | "repository": "sindresorhus/line-column-path", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=20" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "file", 31 | "filepath", 32 | "line", 33 | "column", 34 | "path", 35 | "editor", 36 | "position", 37 | "parse", 38 | "stringify", 39 | "parsing", 40 | "decode", 41 | "encode", 42 | "format" 43 | ], 44 | "dependencies": { 45 | "unicorn-magic": "^0.4.0" 46 | }, 47 | "devDependencies": { 48 | "ava": "^6.4.1", 49 | "tsd": "^0.33.0", 50 | "xo": "^1.2.3" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # line-column-path 2 | 3 | > Parse and stringify file paths with line and column like `unicorn.js:8:14` 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install line-column-path 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {parseLineColumnPath, stringifyLineColumnPath} from 'line-column-path'; 15 | 16 | const parsed = parseLineColumnPath('unicorn.js:8:14'); 17 | //=> {file: 'unicorn.js', line: 8, column: 14} 18 | 19 | stringifyLineColumnPath(parsed); 20 | //=> 'unicorn.js:8:14' 21 | ``` 22 | 23 | ## API 24 | 25 | ### parseLineColumnPath(path) 26 | 27 | #### path 28 | 29 | Type: `string | URL | object` 30 | 31 | The file path to parse. 32 | 33 | Can also be an object that you want to validate and normalize. 34 | 35 | ### stringifyLineColumnPath(path, options?) 36 | 37 | #### path 38 | 39 | Type: `object` 40 | 41 | Object with a `.file` property and optionally a `.line` and `.column` property. 42 | 43 | #### options 44 | 45 | Type: `object` 46 | 47 | ##### file 48 | 49 | Type: `boolean`\ 50 | Default: `true` 51 | 52 | Output the file path. 53 | 54 | Setting this to `false` will result in `8:18` instead of `unicorn.js:8:14`. 55 | 56 | ##### column 57 | 58 | Type: `boolean`\ 59 | Default: `true` 60 | 61 | Output the column. 62 | 63 | Setting this to `false` will result in `unicorn.js:8` instead of `unicorn.js:8:14`. 64 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {toPath} from 'unicorn-magic/node'; 2 | 3 | export function parseLineColumnPath(path) { 4 | path = toPath(path); 5 | 6 | if (typeof path === 'object') { 7 | if (!path.file) { 8 | throw new Error('Missing required `file` property'); 9 | } 10 | 11 | return { 12 | file: toPath(path.file), 13 | line: path.line || 1, 14 | column: path.column || 1, 15 | }; 16 | } 17 | 18 | const match = /^(.*?):(\d+)(?::(\d+))?$/.exec(path); 19 | 20 | if (!match) { 21 | return { 22 | file: path, 23 | line: 1, 24 | column: 1, 25 | }; 26 | } 27 | 28 | if (!match[1]) { 29 | throw new Error('Missing file path'); 30 | } 31 | 32 | return { 33 | file: match[1], 34 | line: Number.parseInt(match[2], 10), 35 | column: Number.parseInt(match[3], 10) || 1, 36 | }; 37 | } 38 | 39 | export function stringifyLineColumnPath(path, options) { 40 | options = { 41 | file: true, 42 | column: true, 43 | ...options, 44 | }; 45 | 46 | if (!path.file) { 47 | throw new Error('Missing required `file` property'); 48 | } 49 | 50 | let result = ''; 51 | 52 | if (options.file) { 53 | result += path.file; 54 | } 55 | 56 | if (path.line) { 57 | result += `:${path.line}`; 58 | } 59 | 60 | if (path.line && path.column && options.column) { 61 | result += `:${path.column}`; 62 | } 63 | 64 | if (!options.file) { 65 | result = result.replace(/^:/, ''); 66 | } 67 | 68 | return result; 69 | } 70 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type PathDescriptor = { 2 | readonly file: string | URL; 3 | readonly line?: number; 4 | readonly column?: number; 5 | }; 6 | 7 | export type PathLike = string | URL | PathDescriptor; 8 | 9 | export type ParsedPath = { 10 | file: string; 11 | line: number; 12 | column: number; 13 | }; 14 | 15 | export type StringifyOptions = { 16 | /** 17 | Output the file path. 18 | 19 | Setting this to `false` will result in `8:18` instead of `unicorn.js:8:14`. 20 | 21 | @default true 22 | */ 23 | readonly file?: boolean; 24 | 25 | /** 26 | Output the column. 27 | 28 | Setting this to `false` will result in `unicorn.js:8` instead of `unicorn.js:8:14`. 29 | 30 | @default true 31 | */ 32 | readonly column?: boolean; 33 | }; 34 | 35 | /** 36 | Parse file paths with line and column like `unicorn.js:8:14`. 37 | 38 | @param path - The file path to parse. Can also be an object that you want to validate and normalize. 39 | 40 | @example 41 | ``` 42 | import {parseLineColumnPath} from 'line-column-path'; 43 | 44 | parseLineColumnPath('unicorn.js:8:14'); 45 | //=> {file: 'unicorn.js', line: 8, column: 14} 46 | ``` 47 | */ 48 | export function parseLineColumnPath(path: PathLike): ParsedPath; 49 | 50 | /** 51 | Stringify file paths. 52 | 53 | @example 54 | ``` 55 | import {stringifyLineColumnPath} from 'line-column-path'; 56 | 57 | stringifyLineColumnPath({file: 'unicorn.js', line: 8, column: 14}); 58 | //=> 'unicorn.js:8:14' 59 | ``` 60 | */ 61 | export function stringifyLineColumnPath( 62 | path: PathDescriptor, 63 | options?: StringifyOptions 64 | ): string; 65 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import url from 'node:url'; 2 | import path from 'node:path'; 3 | import test from 'ava'; 4 | import {parseLineColumnPath, stringifyLineColumnPath} from './index.js'; 5 | 6 | const fixturePath = path.resolve('/Users/sindresorhus/dev/unicorn/x.js'); 7 | const fixtureUrl = url.pathToFileURL(fixturePath); 8 | 9 | test('parse string', t => { 10 | t.deepEqual(parseLineColumnPath('x.js:1:2'), { 11 | file: 'x.js', 12 | line: 1, 13 | column: 2, 14 | }); 15 | 16 | t.deepEqual(parseLineColumnPath(`${fixturePath}:1:2`), { 17 | file: fixturePath, 18 | line: 1, 19 | column: 2, 20 | }); 21 | 22 | t.deepEqual(parseLineColumnPath('x.js:10'), { 23 | file: 'x.js', 24 | line: 10, 25 | column: 1, 26 | }); 27 | 28 | t.deepEqual(parseLineColumnPath('x.js'), { 29 | file: 'x.js', 30 | line: 1, 31 | column: 1, 32 | }); 33 | 34 | t.throws(() => { 35 | parseLineColumnPath(':1:1'); 36 | }, { 37 | message: 'Missing file path', 38 | }); 39 | }); 40 | 41 | test('parse URL', t => { 42 | t.deepEqual(parseLineColumnPath(fixtureUrl), { 43 | file: fixturePath, 44 | line: 1, 45 | column: 1, 46 | }); 47 | }); 48 | 49 | test('parse object', t => { 50 | t.deepEqual(parseLineColumnPath({ 51 | file: 'x.js', 52 | line: 20, 53 | column: 10, 54 | }), { 55 | file: 'x.js', 56 | line: 20, 57 | column: 10, 58 | }); 59 | 60 | t.deepEqual(parseLineColumnPath({ 61 | file: 'x.js', 62 | line: 20, 63 | }), { 64 | file: 'x.js', 65 | line: 20, 66 | column: 1, 67 | }); 68 | 69 | t.deepEqual(parseLineColumnPath({ 70 | file: 'x.js', 71 | }), { 72 | file: 'x.js', 73 | line: 1, 74 | column: 1, 75 | }); 76 | 77 | t.deepEqual(parseLineColumnPath({ 78 | file: fixtureUrl, 79 | }), { 80 | file: fixturePath, 81 | line: 1, 82 | column: 1, 83 | }); 84 | 85 | t.deepEqual(parseLineColumnPath({ 86 | file: fixtureUrl, 87 | line: 20, 88 | column: 10, 89 | }), { 90 | file: fixturePath, 91 | line: 20, 92 | column: 10, 93 | }); 94 | 95 | t.throws(() => { 96 | parseLineColumnPath({noop: 'x'}); 97 | }, { 98 | message: 'Missing required `file` property', 99 | }); 100 | }); 101 | 102 | test('stringify', t => { 103 | t.is(stringifyLineColumnPath({ 104 | file: 'x.js', 105 | line: 20, 106 | column: 10, 107 | }), 'x.js:20:10'); 108 | 109 | t.is(stringifyLineColumnPath({ 110 | file: 'x.js', 111 | line: 20, 112 | }), 'x.js:20'); 113 | 114 | t.is(stringifyLineColumnPath({ 115 | file: 'x.js', 116 | }), 'x.js'); 117 | 118 | t.throws(() => { 119 | stringifyLineColumnPath({noop: 'x'}); 120 | }, { 121 | message: 'Missing required `file` property', 122 | }); 123 | 124 | t.is(stringifyLineColumnPath({ 125 | file: 'x.js', 126 | line: 20, 127 | column: 10, 128 | }, {file: false}), '20:10'); 129 | 130 | t.is(stringifyLineColumnPath({ 131 | file: 'x.js', 132 | line: 20, 133 | column: 10, 134 | }, {column: false}), 'x.js:20'); 135 | }); 136 | --------------------------------------------------------------------------------