Skip to content

Commit

Permalink
Fix deprecated boolean package warning (#597)
Browse files Browse the repository at this point in the history
* feat: Implement isBooleanable  function

* test: Add tests for isBooleanable function

* feat: Implement boolean function

* test: Add tests for boolean function

* refactor: remove boolean package

* Update lib/boolean.ts

Co-authored-by: Bryan Mishkin <[email protected]>

---------

Co-authored-by: Bryan Mishkin <[email protected]>
  • Loading branch information
y-hsgw and bmish authored Dec 22, 2024
1 parent 60d4fdc commit 9873832
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 11 deletions.
56 changes: 56 additions & 0 deletions lib/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Originally from: https://www.npmjs.com/package/boolean

export function boolean(value: unknown): boolean {
switch (typeof value) {
case 'string': {
return ['true', 't', 'yes', 'y', 'on', '1'].includes(
value.trim().toLowerCase(),
);
}

case 'number': {
return value.valueOf() === 1;
}

case 'boolean': {
return value.valueOf();
}

default: {
return false;
}
}
}

export function isBooleanable(value: unknown): boolean {
switch (typeof value) {
case 'string': {
return [
'true',
't',
'yes',
'y',
'on',
'1',
'false',
'f',
'no',
'n',
'off',
'0',
].includes(value.trim().toLowerCase());
}

case 'number': {
return [0, 1].includes(Number(value).valueOf());
}

case 'boolean': {
return true;
}

default: {
return false;
}
}
}
2 changes: 1 addition & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
OPTION_TYPE,
} from './types.js';
import { getCurrentPackageVersion } from './package-json.js';
import { boolean, isBooleanable } from 'boolean';
import { boolean, isBooleanable } from './boolean.js';
import { CONFIG_FORMATS } from './config-format.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/rule-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from './string.js';
import { noCase } from 'change-case';
import { getProperty } from 'dot-prop';
import { boolean, isBooleanable } from 'boolean';
import { boolean, isBooleanable } from './boolean.js';
import Ajv from 'ajv';
import { ConfigFormat } from './config-format.js';

Expand Down
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"dependencies": {
"@typescript-eslint/utils": "^8.0.0",
"ajv": "^8.11.2",
"boolean": "^3.2.0",
"change-case": "^5.0.0",
"commander": "^12.1.0",
"cosmiconfig": "^9.0.0",
Expand Down
61 changes: 61 additions & 0 deletions test/lib/boolean-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { boolean, isBooleanable } from '../../lib/boolean.js';

describe('boolean', function () {
describe('#boolean', function () {
it.each(['true', 't', 'yes', 'y', 'on', '1'])(
'returns true when string is %s',
function (value) {
expect(boolean(value)).toBe(true);
},
);

it('returns true when number is 1', function () {
expect(boolean(1)).toBe(true);
});

it('returns true when boolean is true', function () {
expect(boolean(true)).toBe(true);
});

it.each(['foo', 2, undefined])(
'returns false when value is %p',
function (value) {
expect(boolean(value)).toBe(false);
},
);
});

describe('#isBooleanable', function () {
it.each([
'true',
't',
'yes',
'y',
'on',
'1',
'false',
'f',
'no',
'n',
'off',
'0',
])('returns true when string is %s', function (value) {
expect(isBooleanable(value)).toBe(true);
});

it.each([0, 1])('returns true when number is %i', function (value) {
expect(isBooleanable(value)).toBe(true);
});

it.each([true, false])('returns when boolean is %s', function (value) {
expect(isBooleanable(value)).toBe(true);
});

it.each(['foo', 2, undefined])(
'returns false when value is %p',
function (value) {
expect(isBooleanable(value)).toBe(false);
},
);
});
});

0 comments on commit 9873832

Please sign in to comment.