Skip to content

Commit

Permalink
Merge pull request #8 from hcodes/ts
Browse files Browse the repository at this point in the history
js → ts
  • Loading branch information
hcodes authored May 9, 2020
2 parents 2c38007 + f282b55 commit ee696d1
Show file tree
Hide file tree
Showing 18 changed files with 5,446 additions and 1,554 deletions.
11 changes: 0 additions & 11 deletions .eslintrc

This file was deleted.

9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
22 changes: 22 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Node CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install and test
run: |
npm ci
npm test
env:
CI: true
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
benchmark/
node_modules/
libs/
.nyc_output/
npm-debug.log
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.0.0
- Add typings for TypeScript.
- Drop support for old Node.js < 10.

## v2.1.0
- Add support for `Uint8Array`.
- Updated dev deps in package.json.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Denis Seleznev, [email protected]
Copyright (c) 2020 Denis Seleznev, [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="node" />
/**
* Check if a Node.js Buffer or Uint8Array is UTF-8.
*/
export default function isUtf8(buf?: Buffer | Uint8Array): boolean;
80 changes: 80 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"use strict";
/*
https://tools.ietf.org/html/rfc3629
UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
UTF8-1 = %x00-7F
UTF8-2 = %xC2-DF UTF8-tail
UTF8-3 = %xE0 %xA0-BF UTF8-tail
%xE1-EC 2( UTF8-tail )
%xED %x80-9F UTF8-tail
%xEE-EF 2( UTF8-tail )
UTF8-4 = %xF0 %x90-BF 2( UTF8-tail )
%xF1-F3 3( UTF8-tail )
%xF4 %x80-8F 2( UTF8-tail )
UTF8-tail = %x80-BF
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Check if a Node.js Buffer or Uint8Array is UTF-8.
*/
function isUtf8(buf) {
if (!buf) {
return false;
}
let i = 0;
const len = buf.length;
while (i < len) {
// UTF8-1 = %x00-7F
if (buf[i] <= 0x7F) {
i++;
continue;
}
// UTF8-2 = %xC2-DF UTF8-tail
if (buf[i] >= 0xC2 && buf[i] <= 0xDF) {
// if(buf[i + 1] >= 0x80 && buf[i + 1] <= 0xBF) {
if (buf[i + 1] >> 6 === 2) {
i += 2;
continue;
}
else {
return false;
}
}
// UTF8-3 = %xE0 %xA0-BF UTF8-tail
// UTF8-3 = %xED %x80-9F UTF8-tail
if (((buf[i] === 0xE0 && buf[i + 1] >= 0xA0 && buf[i + 1] <= 0xBF) ||
(buf[i] === 0xED && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x9F)) && buf[i + 2] >> 6 === 2) {
i += 3;
continue;
}
// UTF8-3 = %xE1-EC 2( UTF8-tail )
// UTF8-3 = %xEE-EF 2( UTF8-tail )
if (((buf[i] >= 0xE1 && buf[i] <= 0xEC) ||
(buf[i] >= 0xEE && buf[i] <= 0xEF)) &&
buf[i + 1] >> 6 === 2 &&
buf[i + 2] >> 6 === 2) {
i += 3;
continue;
}
// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail )
// %xF1-F3 3( UTF8-tail )
// %xF4 %x80-8F 2( UTF8-tail )
if (((buf[i] === 0xF0 && buf[i + 1] >= 0x90 && buf[i + 1] <= 0xBF) ||
(buf[i] >= 0xF1 && buf[i] <= 0xF3 && buf[i + 1] >> 6 === 2) ||
(buf[i] === 0xF4 && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x8F)) &&
buf[i + 2] >> 6 === 2 &&
buf[i + 3] >> 6 === 2) {
i += 4;
continue;
}
return false;
}
return true;
}
exports.default = isUtf8;
Loading

0 comments on commit ee696d1

Please sign in to comment.