Skip to content

A collection of features that I wish vanilla JavaScript had, such as clamp, and some features I depend on, such as generating a random integer between two values.

License

Notifications You must be signed in to change notification settings

010ender/QuickieLibrary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QuickieLibrary

What is it?

This is a JavaScript library that is a collection of features that I wish vanilla JavaScript had to streamline production. Features range from efficient math functions to string utilities to advanced type detection. This was first made as a personal library, but then I just decided to give it a home.

Current features

Math

  • lib.randomNumber(min, max) generates a random integer between min and max. Inclusive. Note that this function is not cryptographically secure.
  • lib.coinToss() returns either true or false randomly. Note that this function is not cryptographically secure.
  • lib.clamp(value, min, max) returns value clamped between min and max. Inclusive.
  • lib.lerp(start, end, amount) performs linear interpolation on start and end, based on the weighting factor, amount.
  • lib.map(value, inMin, inMax, outMin, outMax) maps value from the input range defined by inMin and inMax to the output range defined by outMin and outMax. If inMin === inMax, the function will result in a division by zero.
  • lib.areaOfCircleByRadius(radius) returns the area of a circle given its radius. Uses the formula π * r².
  • lib.areaOfCircleByDiameter(diameter) returns the area of a circle given its diameter. Internally converts diameter to radius and applies the formula π * r².
  • lib.circumferenceOfCircleByRadius(radius) returns the circumference of a circle given its radius. Uses the formula 2 * π * r.
  • lib.circumferenceOfCircleByDiameter(diameter) returns the circumference of a circle given its diameter. Uses the formula π * d.

Strings

  • lib.stringIsEmpty(string) returns true if the string is empty (""), and false otherwise.
  • lib.capitalizeFirstLetterOfString(string) returns string, but with the first letter of the string capitalized. For example, "example string" would result in "Example string".
  • lib.slugify(string) alters string into a human-readable URL slug. The full process:
    1. Normalize Unicode
    2. Remove diatrics
    3. String to lowercase
    4. Trim whitespace at start and end
    5. Replace spaces with hyphens
    6. Replace non-word characters except for hyphens
    7. Replace multiple hyphens with one
  • lib.validJSON(string) returns true if string is valid JSON, otherwise returns false.
  • lib.copyTextToClipboard(string) (async function) copies string as text to the clipboard, returning a Promise that is resolved when the clipboard has been updated. Needs "clipboardRead" or "clipboardWrite" permissions and only works in secure contexts.
  • lib.readClipboardAsText() (async function) returns a string of the textual contents of the clipboard. Returns an empty string if the clipboard is empty or does not contain text. Needs "clipboardRead" or "clipboardWrite" permissions and only works in secure contexts.
  • lib.getLetterOfAlphabet(position) returns the letter of the alphabet at position (zero-indexed, range is 0-25) as a string.
  • lib.randomString(length, randomCase, numbers) returns random sequences of letters (eg, hfekjlnoxl if randomCase is false, or QENtbUFmnz if randomCase is true) with numbers if numbers is true, otherwise with no numbers. Default values for both randomCase and numbers are false. `Note that this function is not cryptographically secure.

Objects

  • lib.objectIsEmpty(object) returns true if object has no properties, returns false otherwise.
  • lib.objectPropertiesLength(object) returns the number of enumerable properties in object as a number.
  • lib.isPlainObject(object) determines whether object is a plain object (i.e., not an array, function, or other built-in type).
  • lib.cleanObject(object) returns object that has removed null, undefined, or empty values.
  • lib.falsy(value) determines if value is falsy, returning either false or true.

Time

  • lib.isLeapYear(year) returns true if year is a leap year, returns false otherwise.
  • lib.daysInMonth(month, year) returns the days in month as a number. year is needed to account for leap years.

Array

  • lib.unique(array) returns a new array with only the unique elements from the input array, removing any duplicates.

Files

  • lib.getFile() (async function) shows the file picker, then when the user selects a file, it returns a File object. Will not work in Safari, Firefox, Webview, Samsung Internet, and Deno. Requires a secure context.
  • lib.getFileWithOpts(options) (async function) shows the file picker configured by options, then when the user selects a file, it returns a File object. Refer to Mozilla docs for configuring the file picker: here. If options is not defined, an error is returned. Will not work in Safari, Firefox, Webview, Samsung Internet, and Deno. Requires a secure context.

Battery

  • lib.batteryIsCharging() (async function) returns a Boolean value, true if the device is charging, and false otherwise. Will not work in Safari, Firefox and Webview. Requires a secure context.
  • lib.batteryLevel() (async function) returns the level of the device's battery between 0.0 (fully discharged) and 1.0 (completely charged), as a Number. Will not work in Safari, Firefox and Webview. Requires a secure context.
  • lib.batteryChargingTime() (async function) returns the seconds until the device's battery is fully charged, as a Number. Will not work in Safari, Firefox and Webview. Requires a secure context.
  • lib.batteryDischargingTime() (async function) returns the seconds until the device's battery is fully discharged, as a Number. Will not work in Safari, Firefox and Webview. Requires a secure context.

Misc

  • lib.smartGetType(value) returns the type of value. It is more advanced than JavaScript's typeof operator, as it can recognize arrays, dates, and other types (e.g., new Error returns "error"). This smart type detection function can recognize these JS + Node.js types:
    1. null
    2. undefined
    3. NaN
    4. Infinity
    5. Symbols
    6. BigInts
    7. Functions
    8. Dates
    9. DOM objects
    10. Objects
    11. Arrays
    12. Buffers (specific to Node.js only)
    13. Proxies (outputs as "object")
    14. Revoked proxy (outputs as "proxy:revoked")
  • lib.checkPermissions(permission) returns the state of permission. Check the Mozilla docs for information about the Permissions API.

Usage

Step 1: Download lib.js or the whole repo.
Step 2: Insert <script src="lib.js"></script> into your project. Remember to tweak the path if necessary.
Step 3: Use the library like so: lib.[the name of the function you want]. For example, lib.randomNumber(0, 100).

Examples

Possible outcomes:

console.log(lib.clamp(1000, 0, 100)); would output 100.

console.log(lib.smartGetType(() => {})) would output "function"

console.log(lib.randomNumber(20, 100)) could output 45

About

A collection of features that I wish vanilla JavaScript had, such as clamp, and some features I depend on, such as generating a random integer between two values.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published