Skip to content

Commit

Permalink
feat: remove lodash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiten Bhayani committed Jul 2, 2024
1 parent eb4eac9 commit 31ddaea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"access": "public"
},
"dependencies": {
"lit": "^3.1.0",
"lodash-es": "^4.17.21"
"lit": "3.*"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
Expand Down
24 changes: 18 additions & 6 deletions src/mark-autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LitElement, PropertyValueMap, TemplateResult, css, html } from 'lit';
import { customElement, property, query, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { debounce, filter, get } from 'lodash-es';

type Direction = 'up' | 'down';

Expand All @@ -14,6 +13,20 @@ const KEY_CODES = {
ESCAPE: 27,
};

export const debounce = <T extends (...args: any[]) => any>(fn: T): ((...args: Parameters<T>) => void) => {
let frame: number | null = null;

return (...params: Parameters<T>): void => {
if (frame !== null) {
cancelAnimationFrame(frame);
}

frame = requestAnimationFrame(() => {
fn(...params);
});
};
};

@customElement('mark-autocomplete')
export class MarkAutocomplete<T = unknown> extends LitElement {
static override styles = [
Expand Down Expand Up @@ -138,7 +151,7 @@ export class MarkAutocomplete<T = unknown> extends LitElement {
* When `itemTextProvider` is specified, this is ignored.
*/
@property({ type: String })
textExpression: string;
textExpression: keyof T;

/**
* Provides any Block element to represent list items.
Expand Down Expand Up @@ -204,7 +217,7 @@ export class MarkAutocomplete<T = unknown> extends LitElement {

this._computeItemTextProvider();

this._onUserInteraction = debounce(this._onUserInteraction.bind(this), 100);
this._onUserInteraction = debounce(this._onUserInteraction.bind(this));
}

protected override willUpdate(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
Expand Down Expand Up @@ -450,7 +463,7 @@ export class MarkAutocomplete<T = unknown> extends LitElement {
const lowercaseQuery = query.toLowerCase();
let results = [];

results = filter(items, item => {
results = items.filter(item => {
const itemText = this._getItemText(item).toLowerCase();
return itemText.includes(lowercaseQuery);
});
Expand All @@ -474,11 +487,10 @@ export class MarkAutocomplete<T = unknown> extends LitElement {
}

if (this.textExpression) {
this._itemTextProvider = item => get(item, `${this.textExpression}`);
this._itemTextProvider = item => item?.[this.textExpression] ?? '';
return;
}

this._itemTextProvider = this.itemTextProvider;
}
}

59 changes: 27 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@
resolved "https://registry.yarnpkg.com/@isaacs/string-locale-compare/-/string-locale-compare-1.1.0.tgz#291c227e93fd407a96ecd59879a35809120e432b"
integrity sha512-SQ7Kzhh9+D+ZW9MA0zkYv3VXhIDNx+LzM6EJ+/65I3QY+enU6Itte7E5XX7EWrqLW2FN4n06GWzBnPoC3th2aQ==

"@lit-labs/ssr-dom-shim@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2.tgz#d693d972974a354034454ec1317eb6afd0b00312"
integrity sha512-jnOD+/+dSrfTWYfSXBXlo5l5f0q1UuJo3tkbMDCYA2lKUYq79jaxqtGEvnRoh049nt1vdo1+45RinipU6FGY2g==
"@lit-labs/ssr-dom-shim@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.2.0.tgz#353ce4a76c83fadec272ea5674ede767650762fd"
integrity sha512-yWJKmpGE6lUURKAaIltoPIE/wrbY3TEkqQt+X0m+7fQNnAv0keydnYvbiJFP1PnMhizmIWRWOG5KLhYyc/xl+g==

"@lit/reactive-element@^2.0.0":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-2.0.2.tgz#779ae9d265407daaf7737cb892df5ec2a86e22a0"
integrity sha512-SVOwLAWUQg3Ji1egtOt1UiFe4zdDpnWHyc5qctSceJ5XIu0Uc76YmGpIjZgx9YJ0XtdW0Jm507sDvjOu+HnB8w==
"@lit/reactive-element@^2.0.4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-2.0.4.tgz#8f2ed950a848016383894a26180ff06c56ae001b"
integrity sha512-GFn91inaUa2oHLak8awSIigYz0cU0Payr1rcFsrkf5OJ5eSPxElyZfKh0f2p9FsTiZWXQdWGJeXZICEfXXYSXQ==
dependencies:
"@lit-labs/ssr-dom-shim" "^1.1.2"
"@lit-labs/ssr-dom-shim" "^1.2.0"

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
Expand Down Expand Up @@ -1931,30 +1931,30 @@ lines-and-columns@^1.1.6:
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==

lit-element@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/lit-element/-/lit-element-4.0.2.tgz#1a519896d5ab7c7be7a8729f400499e38779c093"
integrity sha512-/W6WQZUa5VEXwC7H9tbtDMdSs9aWil3Ou8hU6z2cOKWbsm/tXPAcsoaHVEtrDo0zcOIE5GF6QgU55tlGL2Nihg==
lit-element@^4.0.4:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lit-element/-/lit-element-4.0.6.tgz#b9f5b5d68f30636be1314ec76c9a73a6405f04dc"
integrity sha512-U4sdJ3CSQip7sLGZ/uJskO5hGiqtlpxndsLr6mt3IQIjheg93UKYeGQjWMRql1s/cXNOaRrCzC2FQwjIwSUqkg==
dependencies:
"@lit-labs/ssr-dom-shim" "^1.1.2"
"@lit/reactive-element" "^2.0.0"
lit-html "^3.1.0"
"@lit-labs/ssr-dom-shim" "^1.2.0"
"@lit/reactive-element" "^2.0.4"
lit-html "^3.1.2"

lit-html@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lit-html/-/lit-html-3.1.0.tgz#a7b93dd682073f2e2029656f4e9cd91e8034c196"
integrity sha512-FwAjq3iNsaO6SOZXEIpeROlJLUlrbyMkn4iuv4f4u1H40Jw8wkeR/OUXZUHUoiYabGk8Y4Y0F/rgq+R4MrOLmA==
lit-html@^3.1.2:
version "3.1.4"
resolved "https://registry.yarnpkg.com/lit-html/-/lit-html-3.1.4.tgz#30ad4f11467a61e2f08856de170e343184e9034e"
integrity sha512-yKKO2uVv7zYFHlWMfZmqc+4hkmSbFp8jgjdZY9vvR9jr4J8fH6FUMXhr+ljfELgmjpvlF7Z1SJ5n5/Jeqtc9YA==
dependencies:
"@types/trusted-types" "^2.0.2"

lit@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lit/-/lit-3.1.0.tgz#76429b85dc1f5169fed499a0f7e89e2e619010c9"
integrity sha512-rzo/hmUqX8zmOdamDAeydfjsGXbbdtAFqMhmocnh2j9aDYqbu0fjXygjCa0T99Od9VQ/2itwaGrjZz/ZELVl7w==
lit@3.*:
version "3.1.4"
resolved "https://registry.yarnpkg.com/lit/-/lit-3.1.4.tgz#03a72e9f0b1f5da317bf49b1ab579a7132e73d7a"
integrity sha512-q6qKnKXHy2g1kjBaNfcoLlgbI3+aSOZ9Q4tiGa9bGYXq5RBXxkVTqTIVmP2VWMp29L4GyvCFm8ZQ2o56eUAMyA==
dependencies:
"@lit/reactive-element" "^2.0.0"
lit-element "^4.0.0"
lit-html "^3.1.0"
"@lit/reactive-element" "^2.0.4"
lit-element "^4.0.4"
lit-html "^3.1.2"

load-json-file@^4.0.0:
version "4.0.0"
Expand All @@ -1981,11 +1981,6 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

lodash-es@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==

lodash.capitalize@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/lodash.capitalize/-/lodash.capitalize-4.2.1.tgz#f826c9b4e2a8511d84e3aca29db05e1a4f3b72a9"
Expand Down

0 comments on commit 31ddaea

Please sign in to comment.