Skip to content

Commit

Permalink
refactor(bitwise): Improve Typescript Support
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWendelborn committed Sep 15, 2018
1 parent 9f4ddf1 commit 44c7890
Show file tree
Hide file tree
Showing 61 changed files with 623 additions and 279 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exclude_patterns:
- "**/*.test.js"
- '**/*.test.ts'
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mirror to .npmignore
/build/
/coverage/
/node_modules/
# .gitignore-only
/build/
6 changes: 4 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# mirror to .gitignore
/build/
/coverage/
/node_modules/
# .npmignore-only
.travis.yml
.codeclimate.yml
.travis.yml
.gitignore
tsconfig.json
yarn.lock
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
"name": "Florian Wendelborn",
"url": "https://hello.dodekeract.com/npm/bitwise"
},
"babel": {
"plugins": [
"@babel/plugin-proposal-object-rest-spread"
],
"presets": [
"@babel/preset-es2015"
]
},
"bugs": {
"url": "https://github.com/dodekeract/bitwise/issues"
},
Expand Down Expand Up @@ -57,6 +49,7 @@
"license": "MIT",
"main": "index.js",
"name": "bitwise",
"types": "index.d.ts",
"jest": {
"roots": [
"<rootDir>/source"
Expand Down Expand Up @@ -85,9 +78,10 @@
"build:copy": "cp package.json build && cp readme.md build && cp license.md build",
"coverage": "jest --coverage",
"postpublish": "git tag $(node -p \"require('./package.json').version\") && git push --tags",
"prepublish": "yarn run build",
"test": "jest",
"travis": "yarn run coverage; codecov",
"watch": "jest --watch"
},
"version": "1.5.2"
"version": "2.0.0"
}
88 changes: 43 additions & 45 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,35 @@ byte.read(42)
<pre>
<a href="https://yarnpkg.com">yarn</a> add <a href="https://yarnpkg.com/en/package/bitwise">bitwise</a>
</pre>

or

<pre>
<a href="https://npmjs.com">npm</a> i <a href="https://npmjs.com/package/bitwise">bitwise</a>
</pre>

## Table of Contents

* [bits](#bits)
* operations ([and](#bitsand), [nand](#bitsnand), [nor](#bitsnor), [not](#bitsnot), [or](#bitsor), [xnor](#bitsxnor), [xor](#bitsxor))
* reduce operations ([reduceAnd](#bitsreduceand), [reduceNand](#bitsreducenand), [reduceNor](#bitsreducenor), [reduceOr](#bitsreduceor), [reduceXnor](#bitsreducexnor), [reduceXor](#bitsreducexor))
* [toString](#bitstostring)
* [buffer](#buffer)
* [create](#buffercreate)
* [modify](#buffermodify)
* operations ([and](#bufferand), [nand](#buffernand), [nor](#buffernor), [not](#buffernot), [or](#bufferor), [xnor](#bufferxnor), [xor](#bufferxor))
* [read](#bufferread)
* [readCInt](#bufferreadcint)
* [readInt](#bufferreadint)
* [readUInt](#bufferreaduint)
* [byte](#byte)
* [read](#byteread)
* [write](#bytewrite)
* [integer](#integer)
* [getBit](#integergetbit)
* [setBit](#integersetbit)
* [toggleBit](#integertogglebit)
* [nibble](#nibble)
* [read](#nibbleread)
* [write](#nibblewrite)
* [string](#string)
* [toBits](#stringtobits)
- [bits](#bits)
_ operations ([and](#bitsand), [nand](#bitsnand), [nor](#bitsnor), [not](#bitsnot), [or](#bitsor), [xnor](#bitsxnor), [xor](#bitsxor))
_ reduce operations ([reduceAnd](#bitsreduceand), [reduceNand](#bitsreducenand), [reduceNor](#bitsreducenor), [reduceOr](#bitsreduceor), [reduceXnor](#bitsreducexnor), [reduceXor](#bitsreducexor)) \* [toString](#bitstostring)
- [buffer](#buffer)
_ [create](#buffercreate)
_ [modify](#buffermodify)
_ operations ([and](#bufferand), [nand](#buffernand), [nor](#buffernor), [not](#buffernot), [or](#bufferor), [xnor](#bufferxnor), [xor](#bufferxor))
_ [read](#bufferread)
_ [readInt](#bufferreadint)
_ [readUInt](#bufferreaduint)
- [byte](#byte)
_ [read](#byteread)
_ [write](#bytewrite)
- [integer](#integer)
_ [getBit](#integergetbit)
_ [setBit](#integersetbit) \* [toggleBit](#integertogglebit)
- [nibble](#nibble)
_ [read](#nibbleread)
_ [write](#nibblewrite)
- [string](#string) \* [toBits](#stringtobits)

## bits

Expand Down Expand Up @@ -189,7 +187,7 @@ bitwise.bits.xor([1, 0, 0, 0, 1, 1, 0, 1], [0, 1, 1, 0, 0, 1, 0, 0])
Applies the bitwise `AND` operation on the given bits. Returns one bit. Throws if less than 2 bits are given.
```js
bitwise.bits.reduceAnd([1,0,0,0,1,1,0,1])
bitwise.bits.reduceAnd([1, 0, 0, 0, 1, 1, 0, 1])
// 0
```
Expand Down Expand Up @@ -276,7 +274,7 @@ bitwise.bits.toBoolean([0, 1])
### bits.toString
```ts
(bits: Array, spacing = 0, spacer = ' '): String
(bits: Array, spacing = 0, spacer = ' '): string
```
Converts a bit `Array` to a `String`. If defined, inserts `spacer` every `spacing` characters, but never inserts it as the last substring.
Expand Down Expand Up @@ -304,7 +302,7 @@ import create from 'bitwise/buffer/create'
Creates a new buffer and writes the given bits.
```js
const buffer = bitwise.buffer.create([1,1,1,1, 0,0,0,1, 1,0,1,0]);
const buffer = bitwise.buffer.create([1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0])
// Buffer(1111 0001 1010 0000)
```

Expand All @@ -317,8 +315,8 @@ const buffer = bitwise.buffer.create([1,1,1,1, 0,0,0,1, 1,0,1,0]);
Modifies the buffer's bits to equal `newBits` starting at `bitOffset`.
```js
const buffer = Buffer.from('A43A', 'hex');
bitwise.buffer.modify(buffer, [0, 0, 0, 1, 0, 0, 1], 3);
const buffer = Buffer.from('A43A', 'hex')
bitwise.buffer.modify(buffer, [0, 0, 0, 1, 0, 0, 1], 3)
// Buffer(1010 1001 0011 1010)
```

Expand Down Expand Up @@ -427,21 +425,6 @@ bitwise.buffer.read(buffer, 12)
// [0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1]
```

### buffer.readCInt

```ts
(buffer: Buffer, bitOffset = 0, bitLength = 8): Integer
```
Converts a section of a buffer to a complementary integer.
A complementary integer is like an unsigned integer, but always represents negative numbers.
```js
// buffer 11110110
bitwise.buffer.readCInt(buffer, 3, 5)
// -22
```
### buffer.readInt

```ts
Expand Down Expand Up @@ -603,7 +586,7 @@ import toBits from 'bitwise/string/to-bits'
### string.toBits

```ts
(string: String): Array
(string: string): Array
```
Converts a string into an array of bits. Ignores all characters except `1` and `0`.
Expand All @@ -615,13 +598,21 @@ bitwise.string.toBits('10 10 12$%_.0')
## History
### 2.0.0
- refactor to typescript
- remove `bitwise.buffer.readCInt()`
### 1.4.0
- improve `require()` support
### 1.3.0
- add `bits.toBoolean`
### 1.2.0
- add `bits.reduceAnd`
- add `bits.reduceNand`
- add `bits.reduceNor`
Expand All @@ -630,29 +621,36 @@ bitwise.string.toBits('10 10 12$%_.0')
- add `bits.reduceXor`
### 1.1.2
- split up `buffer.operations`
### 1.1.1
- split up `bits.operations`
### 1.1.0
- add `integer.getBit`
- add `integer.setBit`
- add `integer.toggleBit`
### 1.0.0
- rewrite in ES6
- improve utilization of bitwise operators
- improve API (**breaking change**)
### 0.2.0
- Added buffer bitwise operations
### 0.1.2
- Added nor, xnor, nand
- Fixed bitwise operations modifying original array
### 0.1.0
- **Re-ordered the arguments** in readInt, readCInt, readUInt
- Added not, and, or, xor
- Renamed flipBits to not
7 changes: 4 additions & 3 deletions source/bits/and.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Bits } from '../types'
import and from './and'

test('AND', () => {
const bits1 = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2 = [0, 1, 1, 0, 0, 1, 0, 0]
const expected = [0, 0, 0, 0, 0, 1, 0, 0]
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2: Bits = [0, 1, 1, 0, 0, 1, 0, 0]
const expected: Bits = [0, 0, 0, 0, 0, 1, 0, 0]
expect(and(bits1, bits2)).toEqual(expected)
})
7 changes: 4 additions & 3 deletions source/bits/and.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bits } from '../types'
import { Bit, Bits } from '../types'

/**
* Applies the AND operation, expects two arrays of the same size and returns a new one.
Expand All @@ -11,9 +11,10 @@ import { Bits } from '../types'
* @return {Array} [bits1 AND bits2]
*/
export default (bits1: Bits, bits2: Bits): Bits => {
const result = []
const result: Bits = []

for (let i = 0; i < bits1.length; i++) result[i] = bits1[i] & bits2[i]
for (let i: number = 0; i < bits1.length; i++)
result[i] = <Bit>(bits1[i] & bits2[i])

return result
}
7 changes: 4 additions & 3 deletions source/bits/nand.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Bits } from '../types'
import nand from './nand'

test('NAND', () => {
const bits1 = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2 = [0, 1, 1, 0, 0, 1, 0, 0]
const expected = [1, 1, 1, 1, 1, 0, 1, 1]
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2: Bits = [0, 1, 1, 0, 0, 1, 0, 0]
const expected: Bits = [1, 1, 1, 1, 1, 0, 1, 1]
expect(nand(bits1, bits2)).toEqual(expected)
})
7 changes: 4 additions & 3 deletions source/bits/nand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bits } from '../types'
import { Bit, Bits } from '../types'

/**
* Applies the NAND operation, expects two arrays of the same size and returns a new one.
Expand All @@ -11,9 +11,10 @@ import { Bits } from '../types'
* @return {Array} [bits1 NAND bits2]
*/
export default (bits1: Bits, bits2: Bits): Bits => {
const result = []
const result: Bits = []

for (let i = 0; i < bits1.length; i++) result[i] = (bits1[i] & bits2[i]) ^ 1
for (let i: number = 0; i < bits1.length; i++)
result[i] = <Bit>((bits1[i] & bits2[i]) ^ 1)

return result
}
7 changes: 4 additions & 3 deletions source/bits/nor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Bits } from '../types'
import nor from './nor'

test('NOR', () => {
const bits1 = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2 = [0, 1, 1, 0, 0, 1, 0, 0]
const expected = [0, 0, 0, 1, 0, 0, 1, 0]
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2: Bits = [0, 1, 1, 0, 0, 1, 0, 0]
const expected: Bits = [0, 0, 0, 1, 0, 0, 1, 0]
expect(nor(bits1, bits2)).toEqual(expected)
})
7 changes: 4 additions & 3 deletions source/bits/nor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bits } from '../types'
import { Bit, Bits } from '../types'

/**
* Applies the NOR operation, expects two arrays of the same size and returns a new one.
Expand All @@ -11,9 +11,10 @@ import { Bits } from '../types'
* @return {Array} [bits1 NOR bits2]
*/
export default (bits1: Bits, bits2: Bits): Bits => {
const result = []
const result: Bits = []

for (let i = 0; i < bits1.length; i++) result[i] = (bits1[i] | bits2[i]) ^ 1
for (let i: number = 0; i < bits1.length; i++)
result[i] = <Bit>((bits1[i] | bits2[i]) ^ 1)

return result
}
5 changes: 3 additions & 2 deletions source/bits/not.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Bits } from '../types'
import not from './not'

test('NOT', () => {
const bits = [1, 0, 0, 0, 1, 1, 0, 1]
const expected = [0, 1, 1, 1, 0, 0, 1, 0]
const bits: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
const expected: Bits = [0, 1, 1, 1, 0, 0, 1, 0]
expect(not(bits)).toEqual(expected)
})
6 changes: 3 additions & 3 deletions source/bits/not.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bits } from '../types'
import { Bit, Bits } from '../types'

/**
* Flips all given bits and returns the flipped bits.
Expand All @@ -10,9 +10,9 @@ import { Bits } from '../types'
* @return {Array} [NOT bits]
*/
export default (bits: Bits): Bits => {
const result = []
const result: Bits = []

for (let i = 0; i < bits.length; i++) result[i] = bits[i] ^ 1
for (let i: number = 0; i < bits.length; i++) result[i] = <Bit>(bits[i] ^ 1)

return result
}
7 changes: 4 additions & 3 deletions source/bits/or.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Bits } from '../types'
import or from './or'

test('OR', () => {
const bits1 = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2 = [0, 1, 1, 0, 0, 1, 0, 0]
const expected = [1, 1, 1, 0, 1, 1, 0, 1]
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
const bits2: Bits = [0, 1, 1, 0, 0, 1, 0, 0]
const expected: Bits = [1, 1, 1, 0, 1, 1, 0, 1]
expect(or(bits1, bits2)).toEqual(expected)
})
Loading

0 comments on commit 44c7890

Please sign in to comment.