JSPM

@bearz/slices

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 44
  • Score
    100M100P100Q86579F
  • License MIT

Cross runtime module for slices and char slices which creates views over arrays without allocating a new array. Char slices is useful for transforming text without allocating a new string for every change.

Package Exports

  • @bearz/slices
  • @bearz/slices/camelize
  • @bearz/slices/capitalize
  • @bearz/slices/char-array-builder
  • @bearz/slices/char-slice
  • @bearz/slices/dasherize
  • @bearz/slices/ends-with
  • @bearz/slices/equal
  • @bearz/slices/index-of
  • @bearz/slices/last-index-of
  • @bearz/slices/ordinalize
  • @bearz/slices/pascalize
  • @bearz/slices/slice
  • @bearz/slices/starts-with
  • @bearz/slices/titleize
  • @bearz/slices/tokens
  • @bearz/slices/trim
  • @bearz/slices/underscore
  • @bearz/slices/utils

Readme

@bearz/slices

Overview

Slices enable working with slices of an array without resizing or creating new arrays in most cases. Instead, new slices are created that that allows one to work with a segment of the array.

They are not as effecient as go's slice or .NET's Span<T>, but they do provide some benefits for dealing with smaller segments of arrays or character slices.

The CharSlice and ReadOnlyCharSlice are specialized slice types for working with string characters in their uint32 codepoint format and provide string like methods such as trim, indexof, toUpper, toLower, includes, and more without the need to convert them back into strings to perform those operations.

The CharSlice and ReadOnlyCharSlice allow you to work with strings without allocating multiple copies of immuatable strings and allow you to make multiple transforms before converting back to a string.

The case insensitive formats of methods end with "Fold" which are based upon go's SimpleFold and EqualFold methods. For example: equalFold, startsWithFold, endsWithFold, indexOfFold perform case insensitive comparisons over strings or CharSliceLike objects such as CharSlice or Uint32Arrays.

logo

JSR npm version GitHub version

Documentation

Documentation is available on jsr.io

A list of other modules can be found at github.com/bearz-io/js

Usage

import * from slices from '@bearz/slices'

console.log(slices.equalIgnoreCase("left", "LeFT")); // true
console.log(slices.trimEnd("my random text...", ".")); // my random text
console.log(slices.underscore("first-place")); // first_place


var sb = new slices.CharArrayBuilder()
sb.append("test")
   .append(new TextEncoder().encode(": another test"));

// faster
sb.appendString("test")
  .appendUtf8Array(new TextEncoder().encode(": another test"))

console.log(sb.toString())

const slice = new slices.Slice([0, 3, 4], 1);
console.log(slice.at(0)); // 3
console.log(slice.length); // 2

Classes

  • CharArrayBuilder - A builder for character arrays.
  • CharSlice - A slice of characters.
  • ReadOnlySlice - A readonly slice of characters.
  • Slice - A slice of an array, which doesn't create a new array but acts as a view over the array.
  • `ReadOnlySlice - A readonly slice of an array.

Functions

  • camelize - converts a word to camel case.
  • capitalize - capitalizes a word.
  • dasherize - converts a word to hyphen/dash case.
  • endsWith - determines if a string or char array ends with characters.
  • endsWithFold - determines if a string or char array ends with characters using case insensitivity.
  • equalFold - determines if a string or char array with characeters.
  • equal - determines if a string or char array with characters.
  • indexOfFold - determines the index of a character or char array using case insensitivity.
  • indexOf - determines the index of of a character or char array.
  • lastIndexOfFolder - determines the last index of a character or char array using case insensitivity.
  • lastIndex - determines the last index of a character or char array.
  • ordinalize - converts word/number to the ordinal case.
  • pascalize - converts a word to pascal case.
  • startsWith - determines if a string or char array starts with another char array.
  • startsWithFold - determines if a string or char array starts with another char array using case insensitivity.
  • titleize - converts characters into title case.
  • trim - trims the specified characters from the start and end of a char array. defaults to whitespace.
  • trimStart - trims the specified characters from the start of a char array. defaults to whitespace.
  • trimEnd - trims the specified characters from the end of a char array. defaults to whitespace.
  • toCharArray - converts a string to in array of characters/runes represented by a number.
  • toString - converts an array of characters into a string.

License

MIT License