1.0.1 • Published 9 years ago

utfx v1.0.1

Weekly downloads
74,653
License
Apache-2.0
Repository
github
Last release
9 years ago

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.

ParameterTypeDescription
srcfunction():(number | null) | numberCode 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.
dstfunction(number)Bytes destination as a function successively called with the next byte

decodeUTF8(src, dst)

Decodes UTF8 bytes to UTF8 code points.

ParameterTypeDescription
srcfunction():(number | null)Bytes source as a function returning the next byte respectively null if there are no more bytes left.
dstfunction(number)Code points destination as a function successively called with each decoded code point.
@throwsRangeErrorIf a starting byte is invalid in UTF8
@throwsErrorIf the last sequence is truncated. Has an array property bytes holding the remaining bytes.

UTF16toUTF8(src, dst)

Converts UTF16 characters to UTF8 code points.

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

UTF8toUTF16(src, dst)

Converts UTF8 code points to UTF16 characters.

ParameterTypeDescription
srcfunction():(number | null) | numberCode 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.
dstfunction(number)Characters destination as a function successively called with each converted char code.
@throwsRangeErrorIf a code point is out of range

encodeUTF16toUTF8(src, dst)

Converts and encodes UTF16 characters to UTF8 bytes.

ParameterTypeDescription
srcfunction():(number | null)Characters source as a function returning the next char code respectively null if there are no more characters left.
dstfunction(number)Bytes destination as a function successively called with the next byte.

decodeUTF8toUTF16(src, dst)

Decodes and converts UTF8 bytes to UTF16 characters.

ParameterTypeDescription
srcfunction():(number | null)Bytes source as a function returning the next byte respectively null if there are no more bytes left.
dstfunction(number)Characters destination as a function successively called with each converted char code.
@throwsRangeErrorIf a starting byte is invalid in UTF8
@throwsErrorIf the last sequence is truncated. Has an array property bytes holding the remaining bytes.

assertByte(b)

Asserts a byte value.

ParameterTypeDescription
bnumber8bit byte value
@returnsnumberValid byte value
@throwsTypeErrorIf the byte value is invalid
@throwsRangeErrorIf the byte value is out of range

assertCharCode(c)

Asserts an UTF16 char code.

ParameterTypeDescription
cnumberUTF16 char code
@returnsnumberValid char code
@throwsTypeErrorIf the char code is invalid
@throwsRangeErrorIf the char code is out of range

assertCodePoint(cp)

Asserts an UTF8 code point.

ParameterTypeDescription
cpnumberUTF8 code point
@returnsnumberValid code point
@throwsTypeErrorIf the code point is invalid
@throwsRangeErrorIf the code point is out of range

calculateCodePoint(cp)

Calculates the byte length of an UTF8 code point.

ParameterTypeDescription
cpnumberUTF8 code point
@returnsnumberByte length

calculateUTF8(src)

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

ParameterTypeDescription
srcfunction():(number | null)Code points source as a function returning the next code point respectively null if there are no more code points left.
@returnsnumberThe number of UTF8 bytes required

calculateUTF16asUTF8(src)

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

ParameterTypeDescription
srcfunction():(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.

ParameterTypeDescription
a!Array.<number>Array to read from
@returnsfunction():(number | null)Source function returning the next value respectively null if there are no more values left.
@throwsTypeErrorIf the argument is invalid

arrayDestination(a)

Creates a destination function for an array.

ParameterTypeDescription
a!Array.<number>Array to write to
@returnsfunction(number)Destination function successively called with the next value.
@throwsTypeErrorIf the argument is invalid

stringSource(s)

Creates a source function for a string.

ParameterTypeDescription
sstringString to read from
@returnsfunction():(number | null)Source function returning the next char code respectively null if there are no more characters left.
@throwsTypeErrorIf the argument is invalid

stringDestination()

Creates a destination function for a string.

ParameterTypeDescription
@returnsfunction(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.

ParameterTypeDescription
var_args...numberCode points
@returnsstringJavaScript string
@throwsTypeErrorIf arguments are invalid or a code point is invalid
@throwsRangeErrorIf a code point is out of range

codePointAt(s, i)

A polyfill for String#codePointAt.

ParameterTypeDescription
sstringJavaScript string
inumberIndex
@returnsnumber | undefinedCode point or undefined if i is out of range
@throwsTypeErrorIf arguments are invalid

polyfill(override=)

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

ParameterTypeDescription
overridebooleanOverrides 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

1.0.1

9 years ago

1.0.0

10 years ago

0.12.1

10 years ago

0.12.0

10 years ago

0.11.1

10 years ago

0.11.0

10 years ago

0.10.0

10 years ago

0.9.1

10 years ago

0.9.0

10 years ago

0.8.1

10 years ago

0.8.0

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago