Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Port library to TypeScript #37

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
lib/
76 changes: 76 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"extends": ["eslint:recommended", "plugin:node/recommended", "prettier"],
"env": {
"node": true,
"es6": true
},
"rules": {
"eqeqeq": [2, "smart"],
"no-caller": 2,
"dot-notation": 2,
"no-var": 2,
"prefer-const": 2,
"prefer-arrow-callback": [2, { "allowNamedFunctions": true }],
"arrow-body-style": [2, "as-needed"],
"object-shorthand": 2,
"prefer-template": 2,
"one-var": [2, "never"],
"prefer-destructuring": [2, { "object": true }],
"capitalized-comments": 2,
"multiline-comment-style": [2, "starred-block"],
"spaced-comment": 2,
"yoda": [2, "never"],
"curly": [2, "multi-line"],
"no-else-return": 2,

"node/no-unsupported-features/es-syntax": [
2,
{ "ignores": ["modules"] }
]
},
"overrides": [
{
"files": "*.spec.*",
"env": { "jest": true }
},
{
"files": "*.ts",
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint"
],
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"settings": {
"node": {
"tryExtensions": [".js", ".json", ".node", ".ts"]
}
},
"rules": {
"@typescript-eslint/prefer-for-of": 0,
"@typescript-eslint/member-ordering": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-unused-vars": 0,
"@typescript-eslint/no-use-before-define": [
2,
{ "functions": false }
],
"@typescript-eslint/consistent-type-definitions": [
2,
"interface"
],
"@typescript-eslint/prefer-function-type": 2,
"@typescript-eslint/no-unnecessary-type-arguments": 2,
"@typescript-eslint/prefer-string-starts-ends-with": 2,
"@typescript-eslint/prefer-readonly": 2,
"@typescript-eslint/prefer-includes": 2,
"@typescript-eslint/no-unnecessary-condition": 2,
"@typescript-eslint/switch-exhaustiveness-check": 2,
"@typescript-eslint/prefer-nullish-coalescing": 2
}
}
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
lib/
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
coverage/
lib/
src/maps/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- lts/*
sudo: false
- lts/*
after_success: npm run coverage
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) 2013 Felix Böhm
Copyright (c) 2013-2020 Felix Böhm

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
40 changes: 0 additions & 40 deletions index.js

This file was deleted.

91 changes: 64 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,66 @@
{
"name": "bitfield",
"description": "a very simple bitfield implementation using buffers",
"version": "3.0.0",
"author": "Felix Boehm <me@feedic.com>",
"bugs": {
"url": "https://github.com/fb55/bitfield/issues"
},
"devDependencies": {
"standard": "^15.0.0",
"tape": "^5.0.1"
},
"engines": {
"node": ">=8"
},
"keywords": [
"bitfield",
"buffer"
],
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/fb55/bitfield"
},
"scripts": {
"test": "standard && tape tests.js"
}
"name": "bitfield",
"description": "a simple bitfield, compliant with the BitTorrent spec",
"version": "3.0.0",
"author": "Felix Boehm <me@feedic.com>",
"funding": "https://github.com/sponsors/fb55",
"sideEffects": false,
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib/"
},
"files": [
"lib/**/*"
],
"bugs": {
"url": "https://github.com/fb55/bitfield/issues"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@types/node": "^14.11.8",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"coveralls": "*",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^11.1.0",
"jest": "^26.5.3",
"prettier": "^2.0.5",
"ts-jest": "^26.1.0",
"typescript": "^4.0.2"
},
"engines": {
"node": ">=8"
},
"keywords": [
"bitfield",
"buffer",
"bittorrent"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/fb55/bitfield"
},
"scripts": {
"test": "jest --coverage && npm run lint",
"coverage": "cat coverage/lcov.info | coveralls",
"lint": "npm run lint:es && npm run lint:prettier",
"lint:es": "eslint .",
"lint:prettier": "npm run prettier -- --check",
"format": "npm run format:es && npm run format:prettier",
"format:es": "npm run lint:es -- --fix",
"format:prettier": "npm run prettier -- --write",
"prettier": "prettier '**/*.{js,ts,md,json,yml}'",
"build": "tsc",
"prepare": "npm run build"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"prettier": {
"tabWidth": 4
}
}
25 changes: 12 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
# bitfield

a very simple bitfield, compliant with the Bittorrent spec
A simple bitfield, compliant with the BitTorrent spec.

npm install bitfield

#### Example

```js
var Bitfield = require("bitfield");
import Bitfield from "bitfield";

var field = new Bitfield(256); //create a bitfield with 256 bits
const field = new Bitfield(256); // Create a bitfield with 256 bits.

field.set(128); //set the 128th bit
field.set(128, true); //same as above
field.set(128); // Set the 128th bit.
field.set(128, true); // Same as above.

field.get(128); //true
field.get(200); //false (all values are initialised to `false`)
field.get(1e3); //false (out-of-bounds is also false)
field.get(128); // `true`
field.get(200); // `false` (all values are initialised to `false`)
field.get(1e3); // `false` (out-of-bounds is also false)

field.set(128, false); //set the 128th bit to 0 again
field.set(128, false); // Set the 128th bit to 0 again.

field.buffer; //the buffer used by bitfield
field.buffer; // The buffer used by the bitfield.
```

#### Methods

`Bitfield(data)`: `data` can be either a node.js buffer, WebGL Int8Array or numeric array, or a number representing the maximum number of supported bytes.

`Bitfield#get(index)`: Returns a boolean indicating whether the bit is set.

`Bitfield#set(index[, value])`: `value` defaults to true. Sets the bit to `1` for a value of `true` or `0` for `false`.

##### Auto-grow mode
`Bitfield(data, { grow: size })`: If you `set` an index that is out-of-bounds, the Bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given `size` (in bit). If you want the Bitfield to grow indefinitely, pass `Infinity` as the size.

`Bitfield(data, { grow: size })`: If you `set` an index that is out-of-bounds, the Bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given `size` (in bit). If you want the Bitfield to grow indefinitely, pass `Infinity` as the size.

#### Properties

`Bitfield#buffer`: The contents of the bitfield.

`Bitfield#grow`: The passed growth option (defaults to `0`).

## License

MIT