GithubHelp home page GithubHelp logo

jefurry / utfx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dcodeio/utfx

0.0 1.0 0.0 352 KB

A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.

Home Page: http://dcode.io

License: Apache License 2.0

JavaScript 100.00%

utfx's Introduction

utfx - A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.

utfx is a compact library to encode, decode and convert UTF8 / UTF16 in JavaScript using arbitrary sources and destinations through the use of successively called functions, basically eliminating memory overhead.

The standalone library is also capable of using binary strings and arrays (with the usual overhead) and provides polyfills for String.fromCodePoint and String#codePointAt.

API

encodeUTF8(src, dst)

Encodes UTF8 code points to UTF8 bytes.

Parameter Type Description
src function():(number | null) | number Code points source, either as a function returning the next code point respectively null if there are no more code points left or a single numeric code point.
dst function(number) Bytes destination as a function successively called with the next byte

decodeUTF8(src, dst)

Decodes UTF8 bytes to UTF8 code points.

Parameter Type Description
src function():(number | null) Bytes source as a function returning the next byte respectively null if there are no more bytes left.
dst function(number) Code points destination as a function successively called with each decoded code point.
@throws RangeError If a starting byte is invalid in UTF8
@throws Error If the last sequence is truncated. Has an array property bytes holding the remaining bytes.

UTF16toUTF8(src, dst)

Converts UTF16 characters to UTF8 code points.

Parameter Type Description
src function():(number | null) Characters source as a function returning the next char code respectively null if there are no more characters left.
dst function(number) Code points destination as a function successively called with each converted code point.

UTF8toUTF16(src, dst)

Converts UTF8 code points to UTF16 characters.

Parameter Type Description
src function():(number | null) | number Code points source, either as a function returning the next code point respectively null if there are no more code points left or a single numeric code point.
dst function(number) Characters destination as a function successively called with each converted char code.
@throws RangeError If a code point is out of range

encodeUTF16toUTF8(src, dst)

Converts and encodes UTF16 characters to UTF8 bytes.

Parameter Type Description
src function():(number | null) Characters source as a function returning the next char code respectively null if there are no more characters left.
dst function(number) Bytes destination as a function successively called with the next byte.

decodeUTF8toUTF16(src, dst)

Decodes and converts UTF8 bytes to UTF16 characters.

Parameter Type Description
src function():(number | null) Bytes source as a function returning the next byte respectively null if there are no more bytes left.
dst function(number) Characters destination as a function successively called with each converted char code.
@throws RangeError If a starting byte is invalid in UTF8
@throws Error If the last sequence is truncated. Has an array property bytes holding the remaining bytes.

assertByte(b)

Asserts a byte value.

Parameter Type Description
b number 8bit byte value
@returns number Valid byte value
@throws TypeError If the byte value is invalid
@throws RangeError If the byte value is out of range

assertCharCode(c)

Asserts an UTF16 char code.

Parameter Type Description
c number UTF16 char code
@returns number Valid char code
@throws TypeError If the char code is invalid
@throws RangeError If the char code is out of range

assertCodePoint(cp)

Asserts an UTF8 code point.

Parameter Type Description
cp number UTF8 code point
@returns number Valid code point
@throws TypeError If the code point is invalid
@throws RangeError If the code point is out of range

calculateCodePoint(cp)

Calculates the byte length of an UTF8 code point.

Parameter Type Description
cp number UTF8 code point
@returns number Byte length

calculateUTF8(src)

Calculates the number of UTF8 bytes required to store UTF8 code points.

Parameter Type Description
src function():(number | null) Code points source as a function returning the next code point respectively null if there are no more code points left.
@returns number The number of UTF8 bytes required

calculateUTF16asUTF8(src)

Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.

Parameter Type Description
src function():(number | null) Characters source as a function returning the next char code respectively null if there are no more characters left.
@returns !Array.<number> The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.

arraySource(a)

Creates a source function for an array.

Parameter Type Description
a !Array.<number> Array to read from
@returns function():(number | null) Source function returning the next value respectively null if there are no more values left.
@throws TypeError If the argument is invalid

arrayDestination(a)

Creates a destination function for an array.

Parameter Type Description
a !Array.<number> Array to write to
@returns function(number) Destination function successively called with the next value.
@throws TypeError If the argument is invalid

stringSource(s)

Creates a source function for a string.

Parameter Type Description
s string String to read from
@returns function():(number | null) Source function returning the next char code respectively null if there are no more characters left.
@throws TypeError If the argument is invalid

stringDestination()

Creates a destination function for a string.

Parameter Type Description
@returns function(number=):(undefined | string) Destination function successively called with the next char code. Returns the final string when called without arguments.

fromCodePoint(var_args)

A polyfill for String.fromCodePoint.

Parameter Type Description
var_args ...number Code points
@returns string JavaScript string
@throws TypeError If arguments are invalid or a code point is invalid
@throws RangeError If a code point is out of range

codePointAt(s, i)

A polyfill for String#codePointAt.

Parameter Type Description
s string JavaScript string
i number Index
@returns number | undefined Code point or undefined if i is out of range
@throws TypeError If arguments are invalid

polyfill(override=)

Installs utfx as a polyfill for String.fromCodePoint and String#codePointAt if not implemented.

Parameter Type Description
override boolean Overrides an existing implementation if true, defaults to false
@returns !Object.<string,>* utfx namespace

Usage

  • node.js: npm install utfx

    var utfx = require("utfx");
    ...
  • Browser: <script src="/path/to/utfx.min.js"></script>

    var utfx = dcodeIO.utfx;
    ...
  • Require.js/AMD

    require.config({
        "paths": {
            "utfx": "/path/to/utfx.min.js"
        }
    });
    require(["utfx"], function(utfx) {
        ...
    }

Downloads

FAQ and examples

License

Apache License, Version 2.0

utfx's People

Contributors

dcodeio avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.