Skip to content

Commit

Permalink
feat: Convert to TypeScript (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
azu authored Jan 7, 2023
1 parent 1835653 commit f231e9a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

function compare(v1, v2) {
export type CompareFunction = (v1: number, v2: number) => Boolean;

function compare(v1: number, v2: number) {
return v1 < v2;
}

function upperBound(array, value, comp = compare) {
function upperBound(array: number[], value: number, comp: CompareFunction = compare) {
let len = array.length;
let i = 0;

Expand All @@ -43,7 +45,7 @@ function upperBound(array, value, comp = compare) {
return i;
}

function lowerBound(array, value, comp = compare) {
function lowerBound(array: number[], value: number, comp: CompareFunction = compare) {
let len = array.length;
let i = 0;

Expand All @@ -60,7 +62,7 @@ function lowerBound(array, value, comp = compare) {
return i;
}

function binarySearch(array, value, comp = compare) {
function binarySearch(array: number[], value: number, comp: CompareFunction = compare) {
let cursor = lowerBound(array, value, comp);
return cursor !== array.length && !comp(value, array[cursor]);
}
Expand Down
2 changes: 1 addition & 1 deletion test/binary-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import assert from 'assert'
import { binarySearch } from '../src/index.js'
import { binarySearch } from '../src/index'

describe('binarySearch', () => {
it('less', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/lower-bound.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import assert from 'assert'
import { lowerBound } from '../src/index.js'
import { lowerBound } from '../src/index'

describe('lower-bound', () => {
it('less', () => {
Expand Down
5 changes: 3 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": false,
"noEmit": true
"noEmit": true,
"allowJs": true
},
"include": [
"../src/**/*",
"./**/*"
]
}
}
2 changes: 1 addition & 1 deletion test/upper-bound.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import assert from 'assert'
import { upperBound } from '../src/index.js'
import { upperBound } from '../src/index'

describe('upper-bound', () => {
it('less', () => {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowJs": true
},
"include": [
"src/**/*"
Expand Down

0 comments on commit f231e9a

Please sign in to comment.