Skip to content

Commit 96b72da

Browse files
authoredFeb 28, 2025··
refactor: drop lodash (#210)
* refactor: drop lodash * chore: changeset * refactor: uses `sortedLastIndex`
1 parent 754eaf1 commit 96b72da

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed
 

‎.changeset/thin-needles-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"yaml-eslint-parser": minor
3+
---
4+
5+
Replace `lodash` to reduce the package installation size

‎explorer/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"format": "prettier --write src/"
1414
},
1515
"dependencies": {
16-
"lodash-es": "^4.17.21",
1716
"vue": "^3.5.12"
1817
},
1918
"devDependencies": {

‎explorer/vite.config.ts

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export default defineConfig({
1111
resolve: {
1212
alias: {
1313
"@": fileURLToPath(new URL("./src", import.meta.url)),
14-
lodash: fileURLToPath(
15-
new URL("./node_modules/lodash-es", import.meta.url),
16-
),
1714
},
1815
},
1916
});

‎package.json

-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"homepage": "https://github.com/ota-meshi/yaml-eslint-parser#readme",
4747
"dependencies": {
4848
"eslint-visitor-keys": "^3.0.0",
49-
"lodash": "^4.17.21",
5049
"yaml": "^2.0.0"
5150
},
5251
"devDependencies": {
@@ -56,7 +55,6 @@
5655
"@types/benchmark": "^2.1.1",
5756
"@types/eslint": "^9.0.0",
5857
"@types/eslint-visitor-keys": "^3.0.0",
59-
"@types/lodash": "^4.14.167",
6058
"@types/mocha": "^10.0.0",
6159
"@types/node": "^22.0.0",
6260
"@types/semver": "^7.3.10",

‎src/context.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Comment, Locations, Position, Range, Token } from "./ast";
2-
import lodash from "lodash";
32
import type { CST, DocumentOptions } from "yaml";
43
import { ParseError } from ".";
54
import { parserOptionsToYAMLOption } from "./options";
5+
import { sortedLastIndex } from "./utils";
66

77
export class Context {
88
public readonly code: string;
@@ -121,7 +121,8 @@ class LinesAndColumns {
121121
}
122122

123123
public getLocFromIndex(index: number) {
124-
const lineNumber = lodash.sortedLastIndex(this.lineStartIndices, index);
124+
const lineNumber = sortedLastIndex(this.lineStartIndices, index);
125+
125126
return {
126127
line: lineNumber,
127128
column: index - this.lineStartIndices[lineNumber - 1],

‎src/utils.ts

+41
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,44 @@ function getTaggedValue(
205205
${tagText} ${text}`).toJSON();
206206
return value;
207207
}
208+
209+
/**
210+
* Find the insertion position (index) of an item in an array with items sorted
211+
* in ascending order; so that `splice(sortedIndex, 0, item)` would result in
212+
* maintaining the array's sort-ness. The array can contain duplicates.
213+
* If the item already exists in the array the index would be of the *last*
214+
* occurrence of the item.
215+
*
216+
* Runs in O(logN) time.
217+
*
218+
* MIT License | Copyright (c) 2018 remeda | https://remedajs.com/
219+
*
220+
* The implementation is copied from remeda package:
221+
* https://github.com/remeda/remeda/blob/878206eb3e8ec1c7f1300b1909b7aa629810c8bb/src/sortedLastIndex.ts
222+
* https://github.com/remeda/remeda/blob/878206eb3e8ec1c7f1300b1909b7aa629810c8bb/src/internal/binarySearchCutoffIndex.ts#L1
223+
*
224+
* @param data - The (ascending) sorted array.
225+
* @param item - The item to insert.
226+
* @returns Insertion index (In the range 0..data.length).
227+
* @signature
228+
* sortedLastIndex(data, item)
229+
* @example
230+
* sortedLastIndex(['a','a','b','c','c'], 'c') // => 5
231+
*/
232+
export function sortedLastIndex<T>(array: readonly T[], item: T): number {
233+
let lowIndex = 0;
234+
let highIndex = array.length;
235+
236+
while (lowIndex < highIndex) {
237+
const pivotIndex = (lowIndex + highIndex) >>> 1;
238+
const pivot = array[pivotIndex];
239+
240+
if (pivot <= item) {
241+
lowIndex = pivotIndex + 1;
242+
} else {
243+
highIndex = pivotIndex;
244+
}
245+
}
246+
247+
return highIndex;
248+
}

0 commit comments

Comments
 (0)
Please sign in to comment.