Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/2.0.2 #21

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "key-definitions",
"version": "2.0.1",
"version": "2.0.2",
"description": "get a keyboard key definition from event",
"module": "dist/bundle.js",
"types": "dist/src/bundle.d.ts",
"types": "dist/bundle.d.ts",
"files": [
"dist"
],
Expand Down
24 changes: 8 additions & 16 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};
expect(compare(event as KeyboardEvent, LowerCase.A)).toBeTruthy();
});
Expand All @@ -31,8 +30,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};
expect(compare(event as KeyboardEvent, LowerCase.A)).toBeFalsy();
});
Expand All @@ -52,8 +50,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};
expect(compare(event as KeyboardEvent, QUESTION_MARK)).toBeTruthy();
});
Expand All @@ -68,8 +65,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};
expect(compare(event as KeyboardEvent, SHIFT_LEFT)).toBeTruthy();
});
Expand All @@ -84,8 +80,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};
expect(compare(event as KeyboardEvent, SHIFT_RIGHT)).toBeFalsy();
});
Expand All @@ -100,8 +95,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};

expect(compare(event as KeyboardEvent, SPACE)).toBeTruthy();
Expand All @@ -119,8 +113,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};

expect(
Expand All @@ -138,8 +131,7 @@ describe("Test helper compare", () => {
location: 1,
metaKey: false,
repeat: false,
shiftKey: false,
isComposing: false
shiftKey: false
};

expect(compare(event as KeyboardEvent, [SPACE, ALT_LEFT])).toBeFalsy();
Expand Down
43 changes: 35 additions & 8 deletions types/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,52 @@ function isCharacter(x: KeyboardEvent | string | number): x is CHAR {

/**
* check if given value an object that has same properties as KeyInterface
* @param x
* @param x
* @returns {boolean}
*/
function isKeyInterface(x: any): x is KeyInterface {
return x?.keyCode && x?.code && x?.key || (x?.isAltKey || x?.isMetaKey || x?.isShiftKey);
return (
(x?.keyCode && x?.code && x?.key) ||
x?.isAltKey ||
x?.isMetaKey ||
x?.isShiftKey
);
}

/**
* check if given value belongs to Keyboard Event
* @param x
* @param x
* @returns {boolean}
*/
function isKeyboardEvent(x: any): x is KeyboardEvent {
const eventProperties = ["altKey", "ctrlKey", "code", "key", "location", "metaKey", "repeat", "shiftKey", "isComposing"];
function isKeyboardEvent(x: any): x is KeyboardEvent {
const eventProperties = [
"altKey",
"ctrlKey",
"code",
"key",
"location",
"metaKey",
"repeat",
"shiftKey"
];

return eventProperties.every(property => typeof x?.[property] !== "undefined");
}
const eventAdditionalProperties = [
"isDefaultPrevented",
"isPropagationStopped",
"getModifierState"
];

if (["dev", "test"].includes(process.env.NODE_ENV)) {
return eventProperties.every(
(property) => typeof x?.[property] !== "undefined"
);
}

return [...eventProperties, ...eventAdditionalProperties].every(
(property) => typeof x?.[property] !== "undefined"
);
}

export {
isCharacter, isKeyInterface, isKeyboardEvent
isCharacter, isKeyInterface, isKeyboardEvent
};
Loading