Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

batteries-not-included

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batteries-not-included - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

utils/wrap-number.d.ts

14

CHANGELOG.md

@@ -5,2 +5,16 @@ # Changelog

### [0.0.2](https://github.com/unicorn-utterances/batteries-not-included/compare/v0.0.1...v0.0.2) (2020-02-25)
### Features
* added overflow property to keyboard handler ([3f85fdc](https://github.com/unicorn-utterances/batteries-not-included/commit/3f85fdcc9ff2bf2e765585c500b0d2f3421c92dc))
* added wrap number util ([762f1cd](https://github.com/unicorn-utterances/batteries-not-included/commit/762f1cd5ff60274b221eccf6da829b72fac97d7b))
### Bug Fixes
* parameter in name in doc in wrap-number.ts ([249b63b](https://github.com/unicorn-utterances/batteries-not-included/commit/249b63bebe1816655dd64cc1acf7f57875b0613e))
* updated overflow to work on keyboard handler ([eb50de0](https://github.com/unicorn-utterances/batteries-not-included/commit/eb50de0c401d98f84a5c9628c6d34c6cef311eb1))
### [0.0.1](https://github.com/unicorn-utterances/batteries-not-included/compare/v0.0.1-alpha.5...v0.0.1) (2020-02-24)

@@ -7,0 +21,0 @@

2

package.json
{
"name": "batteries-not-included",
"version": "0.0.1",
"version": "0.0.2",
"description": "A utilities library with a batteries-not-included philosophy",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -25,10 +25,13 @@ /**

runOnIndexChange?: UseKeyboardListNavigationSubmitFn;
wrapOnOverflow?: boolean;
}
/**
* @param parentRef - The parent ref to bind the event handling to
* @param maxLength - The maximum number that can be bound to
* @param enable - Disable event handling
* @param [runOnIndexChange] - An optional function to hook into the event handler logic
* @param $1
* @param $1.maxLength - The maximum number that can be bound to
* @param $1.enable - Disable event handling
* @param [$1.wrapOnOverflow] - If true, when max + 1 is reached, go to zero, etc. Defaults false
* @param [$1.runOnIndexChange] - An optional function to hook into the event handler logic
*/
export declare const useKeyboardListNavigation: (parentRef: RefObject<any>, { maxLength, enable, runOnIndexChange }: UseKeyboardListNavigationOptions) => {
export declare const useKeyboardListNavigation: (parentRef: RefObject<any>, { maxLength, enable, runOnIndexChange, wrapOnOverflow }: UseKeyboardListNavigationOptions) => {
focusedIndex: number;

@@ -35,0 +38,0 @@ selectIndex: (i: number, e?: KeyboardSyntheticEvent | undefined) => void;

@@ -24,8 +24,12 @@ "use strict";

* @param parentRef - The parent ref to bind the event handling to
* @param maxLength - The maximum number that can be bound to
* @param enable - Disable event handling
* @param [runOnIndexChange] - An optional function to hook into the event handler logic
* @param $1
* @param $1.maxLength - The maximum number that can be bound to
* @param $1.enable - Disable event handling
* @param [$1.wrapOnOverflow] - If true, when max + 1 is reached, go to zero, etc. Defaults false
* @param [$1.runOnIndexChange] - An optional function to hook into the event handler logic
*/
exports.useKeyboardListNavigation = (parentRef, { maxLength = Infinity, enable = true, runOnIndexChange }) => {
exports.useKeyboardListNavigation = (parentRef, { maxLength = Infinity, enable = true, runOnIndexChange, wrapOnOverflow = false }) => {
const [focusedIndex, setFocusedIndex] = react_1.useState(0);
// Select whether to allow wrapping behavior or to restrict index movement within a numerical range
const numberValidationFn = wrapOnOverflow ? utils_1.wrapNumber : utils_1.normalizeNumber;
const maxIndex = maxLength - 1;

@@ -47,7 +51,7 @@ // Arrow key handler

event.preventDefault();
_newIndex = utils_1.normalizeNumber(focusedIndex + 1, 0, maxIndex);
_newIndex = numberValidationFn(focusedIndex + 1, 0, maxIndex);
break;
case "ArrowUp":
event.preventDefault();
_newIndex = utils_1.normalizeNumber(focusedIndex - 1, 0, maxIndex);
_newIndex = numberValidationFn(focusedIndex - 1, 0, maxIndex);
break;

@@ -83,3 +87,3 @@ case "Home":

const selectIndex = (i, e) => {
setFocusedIndex(utils_1.normalizeNumber(i, 0, maxIndex));
setFocusedIndex(numberValidationFn(i, 0, maxIndex));
if (runOnIndexChange) {

@@ -86,0 +90,0 @@ if (e && e.persist)

@@ -47,10 +47,9 @@ "use strict";

const runOnIndexChange = (kbEvent, focusedIndex, newIndex) => {
var _a, _b, _c, _d, _e, _f, _g;
// If arrow keys were handled,
if (newIndex !== undefined) {
// We're selecting using mouse and not holding shift, select only one
const isMouseEvent = ((_a = kbEvent) === null || _a === void 0 ? void 0 : _a.nativeEvent) instanceof window.MouseEvent ||
const isMouseEvent = (kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.nativeEvent) instanceof window.MouseEvent ||
(window.TouchEvent &&
((_b = kbEvent) === null || _b === void 0 ? void 0 : _b.nativeEvent) instanceof window.TouchEvent);
if (isMouseEvent && !((_c = kbEvent) === null || _c === void 0 ? void 0 : _c.shiftKey)) {
(kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.nativeEvent) instanceof window.TouchEvent);
if (isMouseEvent && !(kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.shiftKey)) {
markAsSelected(newIndex, newIndex);

@@ -61,5 +60,5 @@ resetLastUsedKeyboard();

// If shift or shift+ctrl were being handled, mark the items as selected
const isKeyboardSelecting = ["Home", "End"].includes(((_d = kbEvent) === null || _d === void 0 ? void 0 : _d.key) || "")
? ((_e = kbEvent) === null || _e === void 0 ? void 0 : _e.shiftKey) && ((_f = kbEvent) === null || _f === void 0 ? void 0 : _f.ctrlKey)
: (_g = kbEvent) === null || _g === void 0 ? void 0 : _g.shiftKey;
const isKeyboardSelecting = ["Home", "End"].includes((kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.key) || "")
? (kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.shiftKey) && (kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.ctrlKey)
: kbEvent === null || kbEvent === void 0 ? void 0 : kbEvent.shiftKey;
// If a single item is selected, go ahead and toggle it

@@ -66,0 +65,0 @@ if (isKeyboardSelecting) {

export * from "./normalize-number";
export * from "./tablize";
export * from "./wrap-number";

@@ -8,1 +8,2 @@ "use strict";

__export(require("./tablize"));
__export(require("./wrap-number"));

@@ -11,2 +11,3 @@ These packages are meant for non-framework based code. This means that every utility in this subpackage should be able to be used

* [Normalize Number](#normalize-number)
* [Wrap Number](#wrap-number)

@@ -47,3 +48,35 @@ ------

### Wrap Number
| File | Function |
| ------------------ | ------------------ |
| `wrap-number` | `wrapNumber` |
As a result, you're able to import either from:
```javascript
import {wrapNumber} from 'batteries-not-included/utils';
import {wrapNumber} from 'batteries-not-included/utils/wrap-number';
```
However, due to potential breaking APIs in the future, it is highly suggested to use the first of the two results.
The function takes three parameters:
1) The number to check the value of
2) The minimum value that the number must be
3) The maximum value that the number must be
All three of these parameters must be numbers
If the number is less than the minimum, the maximum number will be returned
If the number is more than the maximum, the minimum number will be returned
#### Examples
```javascript
wrapNumber(5, 3, 10); // 5
wrapNumber(1, 3, 10); // 10
wrapNumber(18, 3, 10); // 3
```
### Make an ASCII Table From 2D Array

@@ -50,0 +83,0 @@

@@ -6,2 +6,2 @@ /**

*/
export declare const tablize: <T extends any>(twoDimentionalArray: T[][]) => string;
export declare const tablize: <T>(twoDimentionalArray: T[][]) => string;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc