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

@microsoft/fast-web-utilities

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@microsoft/fast-web-utilities

FAST web utilities

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
106K
increased by9.14%
Maintainers
1
Weekly downloads
 
Created
Source

FAST Web utilities

This package is a collection of utilities intended to be used for web projects.

Installation

npm i --save @microsoft/fast-web-utilities

Usage

DOM utilities

getKeyCode

The getKeyCode function gets the numeric key code associated with a keyboard event. This method is for use with DOM level 3 events that still use the deprecated keyCode property.

import { getKeyCode } from "@microsoft/fast-web-utilities";

handleKeyPress = (e) => {
    let keyCode = getKeyCode(e);

    // Do something based on keyCode value
}

HTML utilities

getClientRectWithMargin

The getClientRectWithMargin function gets the client bounding rectangle including any margins of an element.

import { getClientRectWithMargin } from "@microsoft/fast-web-utilities";

const itemWidth = getClientRectWithMargin(item).width;
const itemHeight = getClientRectWithMargin(item).height;
convertStylePropertyPixelsToNumber

The convertStylePropertyPixelsToNumber function will convert a property value from an elements computed style from pixels to a number value.

import { convertStylePropertyPixelsToNumber } from "@microsoft/fast-web-utilities";

const elementTopMargin = convertStylePropertyPixelsToNumber(style, "margin-top");

Key code utilities

KeyCodes (enum)
import { KeyCodes } from "@microsoft/fast-web-utilities";

handleKeyPress = (e) => {
    const keyCode = getKeyCode(e);

    switch (keyCode) {
        case KeyCodes.space:
        case KeyCodes.enter:
            // Do something if keycode matches
            break;
    }
}

Localization utilities

isRTL

The isRTL utility determines if a string which should correspond to a language or language-locale is right to left.

import { isRTL } from "@microsoft/fast-web-utilities";

isRTL("en");
Typescript enum

The Direction enum contains the ltr and rtl enum for use in a Typescript project.

import { Direction } from "@microsoft/fast-web-utilities";

let direction: Direction = Direction.ltr;

Number utilities

Limit

The limit function ensures that a value is between a min and max value. If the value is lower than min, min will be returned. If the value is greater than max, max will be retured.

import { limit } from "@microsoft/fast-web-utilities";
const incomingNumber; // 11 
const setNumberByLimit = limit(0, 10, incomingNumber); // returns 10
wrapInBounds

The wrapInBounds function keeps a given value within the bounds of a min and max value. If the value is larger than the max, the minimum value will be returned. If the value is smaller than the minimum, the maximum will be returned. Otherwise, the value is returned un-changed.

import { wrapInBounds } from "@microsoft/fast-web-utilities";
const slides; // 5
const index; // 5
const activeIndex = wrapInBounds(0, this.slides.length - 1, index) // returns 0

String utilities

Format

The format function builds a string from a format specifier and replacement parameters.

import { format } from "@microsoft/fast-web-utilities";

const formatterString = "View {0} {1}";

const newString = format(formatterString, "page", "4")); // "View page 4"
startsWith

The startsWith function checks to see if one string starts with another. The function is case sensitive.

import { startsWith } from "@microsoft/fast-web-utilities";

const matchIsFalse = startsWith("HelloWorld", "World"); // false
const matchIsTrue = startsWith("HelloWorld", "Hello"); // true
isNullOrWhiteSpace

The isNullOrWhiteSpace function determines if the specified string is undefined, null, empty, or whitespace. The function returns true if the value is undefined, null, empty, or whitespace, otherwise false.

import { isNullOrWhiteSpace } from "@microsoft/fast-web-utilities";

const myAnchor = document.querySelector("#id");
const checkWhitespace = isNullOrWhiteSpace(myAnchor.href);
pascalCase

The pascalCase function converts a string to Pascal Case

import { pascalCase } from "@microsoft/fast-web-utilities";

const hyphenatedToPascal = pascalCase("my-string");
const uppercaseToPascal = pascalCase("MY STRING");
const whitespaceToPascal = pascalCase(" my string ");

FAQs

Package last updated on 25 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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