Skip to content

Provides utility scripts that are useful when creating a website.(Webサイト実装時に多用しそうなUtilityスクリプトを提供します)

License

Notifications You must be signed in to change notification settings

TsubasaHiga/umaki

Repository files navigation

umaki

NPM Version NPM Type Definitions NPM Downloads

character

A package that allows you to import frequently used utilities for web development in one place. Primarily, it includes utilities that I often use. If you have any requests for the contents or types of utilities, please open an issue.

Usage

import { foo } from 'umaki'

List of Utilities

Control

bgScrollStop

A function that stops scrolling.

import { bgScrollStop } from 'umaki'

bgScrollStop() // scroll stop
bgScrollStop(false) // scroll start

View file →

pd

A function that prevents the default event behavior.

import { pd } from 'umaki'

document.getElementById('myElement').addEventListener('click', pd)

View file →

scrollToHash (Promise)

A function that scrolls to a specific hash position.

import { scrollToHash } from 'umaki'

;(async () => {
  await scrollToHash('#target')
  console.log('Scrolled!')

  // smooth scroll
  await scrollToHash('#target', true)

  // smooth scroll + offset
  await scrollToHash('#target', true, 100)
})()

View file →

videoPlayControl

A function that controls the playback of a video element.

import { videoPlayControl } from 'umaki'

const videoElement = document.getElementById('myVideo')
videoPlayControl(videoElement, true) // play
videoPlayControl(videoElement, false) // pause

View file →

Convert

changeDateStringToSpecificFormat

A function that converts a date string to a specific format.

import { changeDateStringToSpecificFormat } from 'umaki'

const date = '2023-10-05'
const formattedDate = changeDateStringToSpecificFormat(date, 'MM/DD/YYYY')
console.log(formattedDate) // '10/05/2023'

View file →

jsonStringToJsonObject

A function that converts a JSON string to a JSON object.

import { jsonStringToJsonObject } from 'umaki'

const jsonString = '{"name": "John", "age": 30}'
const jsonObject = jsonStringToJsonObject(jsonString)
console.log(jsonObject) // { name: 'John', age: 30 }

View file →

EventControl

debounce

A function that limits the number of times a function is called to at most one time over a specified time period.

import { debounce } from 'umaki'

const debouncedFunction = debounce(() => {
  console.log('Debounced!')
}, 300)

window.addEventListener('resize', debouncedFunction)

View file →

throttle

A function that limits the number of times a function is called to a maximum of once in a specified period.

import { throttle } from 'umaki'

const throttledFunction = throttle(() => {
  console.log('Throttled!')
}, 300)

window.addEventListener('scroll', throttledFunction)

View file →

Get

getAspectRatio

A function that returns the aspect ratio of the specified width and height.

import { getAspectRatio } from 'umaki'

const aspectRatio = getAspectRatio(1920, 1080)
console.log(aspectRatio) // { w: 16, h: 9 }

View file →

getClassNames

A function that retrieves the class names of the specified HTML element as an array.

import { getClassNames } from 'umaki'

const element = document.createElement('div')
element.className = 'class1 class2 class3'
const classNames = getClassNames(element)
console.log(classNames) // ['class1', 'class2', 'class3']

View file →

getDocumentHeight

A function that retrieves the height of the document.

import { getDocumentHeight } from 'umaki'

const height = getDocumentHeight()
console.log(height)

View file →

getEventPaths

A function that retrieves the event paths.

import { getEventPaths } from 'umaki'

document.addEventListener('click', (event) => {
  const paths = getEventPaths(event)
  console.log(paths)
})

View file →

getGcd

A function that calculates the greatest common divisor of two numbers.

import { getGcd } from 'umaki'

const gcd = getGcd(48, 18)
console.log(gcd) // 6

View file →

getOrientation

A function that retrieves the current orientation of the device.

import { getOrientation } from 'umaki'

const orientation = getOrientation()
console.log(orientation) // 'landscape' or 'portrait'

View file →

getParentList

A function that recursively retrieves the parent elements of the specified HTML element.

import { getParentList } from 'umaki'

const element = document.createElement('div')
const parentList = getParentList(element)
console.log(parentList)

View file →

getQueryParams

A function that retrieves the query parameters from the URL. This is a wrapper around query-string, so you can specify options in the second argument.

import { getQueryParams } from 'umaki'

const params = getQueryParams('test') // https://example.com?test=123
console.log(params) // 123

// Example of specifying options
const params = getQueryParams('test', { parseNumbers: false })
console.log(params) // '123'

View file →

getRem

A function that converts a pixel value to rem units.

import { getRem } from 'umaki'

const remValue = getRem(16)
console.log(remValue) // '1rem'

View file →

getScrollbarWidth

A function that retrieves the width of the scrollbar.

import { getScrollbarWidth } from 'umaki'

const scrollbarWidth = getScrollbarWidth()
console.log(scrollbarWidth) // 15

View file →

getSessionStorage

A function that retrieves a value from session storage.

import { getSessionStorage } from 'umaki'

const value = getSessionStorage('testKey')
console.log(value) // 'testValue'

View file →

getStringLength

A function that retrieves the length of a string (considering Unicode characters).

import { getStringLength } from 'umaki'

const length = getStringLength('こんにちは')
console.log(length) // 5

View file →

getStylePropertyValue

A function that retrieves the value of the specified CSS custom property.

import { getStylePropertyValue } from 'umaki'

const value = getStylePropertyValue('--custom-property')
console.log(value)

View file →

getStylePropertyValueToNumber

A function that retrieves the value of the specified CSS custom property as a number.

import { getStylePropertyValueToNumber } from 'umaki'

const value = getStylePropertyValueToNumber('--custom-property')
console.log(value)

View file →

getUaData

A function that retrieves user agent information.

import { getUaData } from 'umaki'

const uaData = getUaData()
console.log(uaData)
// {
//   browserName: 'chrome',
//   browserVersion: '91.0.4472.124',
//   browserEngine: 'blink',
//   osName: 'windows',
//   type: 'desktop',
//   touchSupport: false
// }

View file →

Is

isBetweenDateTime

A function that checks if the current date is between two specified dates.

import { isBetweenDateTime } from 'umaki'

const dateA = '2023-10-01'
const dateB = '2023-10-10'
const result = isBetweenDateTime(dateA, dateB)
console.log(result) // true or false

View file →

isExistAllElements

A function that checks if all elements exist.

import { isExistAllElements } from 'umaki'

const elements = [document.createElement('div'), document.createElement('span')]
const result = isExistAllElements(elements)
console.log(result) // true or false

View file →

isIpad

A function that checks if the device is an iPad.

import { isIpad } from 'umaki'

const result = isIpad()
console.log(result) // true or false

View file →

isKeyExists

A function that checks if a specific key exists in an object.

import { isKeyExists } from 'umaki'

const obj = { a: 1, b: 2 }
const result = isKeyExists(obj, 'a')
console.log(result) // true or false

View file →

isSafari

A function that checks if the browser is Safari.

import { isSafari } from 'umaki'

const result = isSafari()
console.log(result) // true or false

View file →

isScrollable

A function that checks if an element is scrollable.

import { isScrollable } from 'umaki'

const element = document.createElement('div')
element.style.overflow = 'auto'
element.innerHTML = '<div style="height: 200px;"></div>'
const result = isScrollable(element)
console.log(result) // true or false

View file →

isTouchSupport

A function that checks if the device supports touch.

import { isTouchSupport } from 'umaki'

const result = isTouchSupport()
console.log(result) // true or false

View file →

Remove

removeAllHtmlTags

A function that removes all HTML tags from a string.

import { removeAllHtmlTags } from 'umaki'

const input = '<p>Hello <strong>World</strong>!</p>'
const output = removeAllHtmlTags(input)
console.log(output) // 'Hello World!'

View file →

removeAttribute

A function that removes an attribute from the specified HTML element.

import { removeAttribute } from 'umaki'

const element = document.createElement('div')
element.setAttribute('data-test', 'value')
removeAttribute(element, 'data-test')
console.log(element.hasAttribute('data-test')) // false

View file →

removeSessionStorage

A function that removes an item with the specified key from session storage.

import { removeSessionStorage } from 'umaki'

const key = 'testKey'
removeSessionStorage(key)
console.log(sessionStorage.getItem(key)) // null

View file →

removeStylePropertyValue

A function that removes the specified CSS custom property.

import { removeStylePropertyValue } from 'umaki'

const key = '--custom-property'
removeStylePropertyValue(key)
console.log(getComputedStyle(document.documentElement).getPropertyValue(key)) // ''

View file →

Set

set100vh

A function that sets a CSS variable to 100vh to address viewport unit issues on mobile devices.

import { set100vh } from 'umaki'

set100vh()

View file →

set100vw

A function that sets a CSS variable to 100vw minus the scrollbar width.

import { set100vw } from 'umaki'

set100vw()

View file →

setAttribute

A function that sets an attribute on the specified HTML element.

import { setAttribute } from 'umaki'

const element = document.createElement('div')
setAttribute(element, 'data-test', 'value')
console.log(element.getAttribute('data-test')) // 'value'

View file →

setScrollPositionToCenter

A function that adjusts the horizontal scroll position of the root element to center the target element.

import { setScrollPositionToCenter } from 'umaki'

const rootElement = document.getElementById('root')
const targetElement = document.getElementById('target')
setScrollPositionToCenter(rootElement, targetElement)

View file →

setSessionStorage

A function that sets a value in session storage.

import { setSessionStorage } from 'umaki'

const key = 'testKey'
const value = 'testValue'
setSessionStorage(key, value)
console.log(sessionStorage.getItem(key)) // 'testValue'

View file →

setStylePropertyValue

A function that sets a CSS custom property on the root element.

import { setStylePropertyValue } from 'umaki'

const key = '--custom-color'
const value = 'blue'
setStylePropertyValue(key, value)
console.log(getComputedStyle(document.documentElement).getPropertyValue(key)) // 'blue'

View file →

To

toBoolean

A function that converts a string to a boolean value.

import { toBoolean } from 'umaki'

console.log(toBoolean('true')) // true
console.log(toBoolean('false')) // false
console.log(toBoolean('random')) // false

View file →

toPositiveNumber

A function that converts a number to a positive number. Returns the absolute value if the number is negative.

import { toPositiveNumber } from 'umaki'

console.log(toPositiveNumber(5)) // 5
console.log(toPositiveNumber(-5)) // 5
console.log(toPositiveNumber(0)) // 0
console.log(toPositiveNumber(-3.14)) // 3.14
console.log(toPositiveNumber(3.14)) // 3.14

View file →

Transform

wrapTextWithSpans

A function that wraps each character of the text content of an HTML element with individual <span> elements.

import { wrapTextWithSpans } from 'umaki'

const element = document.createElement('div')
element.textContent = 'hello'
wrapTextWithSpans(element)
console.log(element.innerHTML) // '<span>h</span><span>e</span><span>l</span><span>l</span><span>o</span>'

View file →

Wait

sleep (Promise)

A function that pauses execution for a specified amount of time.

import { sleep } from 'umaki'

;(async () => {
  console.log('Start')
  await sleep(1) // 1ms wait
  console.log('End')
})()

View file →

waitForAllMediaLoaded (Promise)

A function that waits until all images and videos in the document are fully loaded.

import { waitForAllMediaLoaded } from 'umaki'

;(async () => {
  const allMediaLoaded = await waitForAllMediaLoaded()
  console.log(allMediaLoaded) // true or false

  // first view only
  const firstViewMediaLoaded = await waitForAllMediaLoaded(true)
  console.log(firstViewMediaLoaded) // true or false
})()

View file →

Using for framework and tools

Specific usage for frameworks or various tools is described below.

Astro or Vite

When using with Astro for SSR, add umaki to vite.ssr.noExternal.

// astro.config.ts
export default defineConfig(({ mode }) => {
  return {
    vite: {
      ssr: {
        noExternal: ['umaki']
      }
    }
  }
})

About

Provides utility scripts that are useful when creating a website.(Webサイト実装時に多用しそうなUtilityスクリプトを提供します)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published