Skip to content

Commit

Permalink
feat: add is-number-object
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 17, 2024
1 parent 2f694a5 commit 39dd37c
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 4 deletions.
11 changes: 7 additions & 4 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import isBooleanObject from './is-boolean-object/index.js';

import isDateObject from './is-date-object/index.js';

import isNumberObject from './is-number-object/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject
};
67 changes: 67 additions & 0 deletions codemods/is-number-object/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'is-number-object',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

removeImport('is-number-object', root, j);

// Replace all calls to isNumber with Object.prototype.toString.call
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'isNumber',
},
})
.replaceWith((path) => {
const arg = path.node.arguments[0];
return j.callExpression(
j.memberExpression(
j.memberExpression(
j.memberExpression(
j.identifier('Object'),
j.identifier('prototype'),
),
j.identifier('toString'),
),
j.identifier('call'),
),
[arg],
);
})
.forEach((path) => {
const parent = path.parent.node;
if (j.BinaryExpression.check(parent)) {
parent.operator = '===';
parent.right = j.literal('[object Number]');
} else if (
j.CallExpression.check(parent) &&
parent.arguments.length === 1
) {
const newExpression = j.binaryExpression(
'===',
path.node,
j.literal('[object Number]'),
);
parent.arguments[0] = newExpression;
}
});

return root.toSource({ quote: 'single' });
},
};
}
18 changes: 18 additions & 0 deletions test/fixtures/is-number-object/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var assert = require('assert');

assert.notOk(Object.prototype.toString.call(undefined) === '[object Number]');
assert.notOk(Object.prototype.toString.call(null) === '[object Number]');
assert.notOk(Object.prototype.toString.call(false) === '[object Number]');
assert.notOk(Object.prototype.toString.call(true) === '[object Number]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Number]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Number]');
assert.notOk(Object.prototype.toString.call([]) === '[object Number]');
assert.notOk(Object.prototype.toString.call({}) === '[object Number]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Number]');

assert.ok(Object.prototype.toString.call(42) === '[object Number]');
assert.ok(Object.prototype.toString.call(NaN) === '[object Number]');
assert.ok(Object.prototype.toString.call(Infinity) === '[object Number]');
assert.ok(Object.prototype.toString.call(new Number(42)) === '[object Number]');
19 changes: 19 additions & 0 deletions test/fixtures/is-number-object/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var isNumber = require('is-number-object');
var assert = require('assert');

assert.notOk(isNumber(undefined));
assert.notOk(isNumber(null));
assert.notOk(isNumber(false));
assert.notOk(isNumber(true));
assert.notOk(isNumber('foo'));
assert.notOk(isNumber(function () {}));
assert.notOk(isNumber([]));
assert.notOk(isNumber({}));
assert.notOk(isNumber(/a/g));
assert.notOk(isNumber(new RegExp('a', 'g')));
assert.notOk(isNumber(new Date()));

assert.ok(isNumber(42));
assert.ok(isNumber(NaN));
assert.ok(isNumber(Infinity));
assert.ok(isNumber(new Number(42)));
18 changes: 18 additions & 0 deletions test/fixtures/is-number-object/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var assert = require('assert');

assert.notOk(Object.prototype.toString.call(undefined) === '[object Number]');
assert.notOk(Object.prototype.toString.call(null) === '[object Number]');
assert.notOk(Object.prototype.toString.call(false) === '[object Number]');
assert.notOk(Object.prototype.toString.call(true) === '[object Number]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Number]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Number]');
assert.notOk(Object.prototype.toString.call([]) === '[object Number]');
assert.notOk(Object.prototype.toString.call({}) === '[object Number]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Number]');

assert.ok(Object.prototype.toString.call(42) === '[object Number]');
assert.ok(Object.prototype.toString.call(NaN) === '[object Number]');
assert.ok(Object.prototype.toString.call(Infinity) === '[object Number]');
assert.ok(Object.prototype.toString.call(new Number(42)) === '[object Number]');
18 changes: 18 additions & 0 deletions test/fixtures/is-number-object/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert';

assert.notOk(Object.prototype.toString.call(undefined) === '[object Number]');
assert.notOk(Object.prototype.toString.call(null) === '[object Number]');
assert.notOk(Object.prototype.toString.call(false) === '[object Number]');
assert.notOk(Object.prototype.toString.call(true) === '[object Number]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Number]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Number]');
assert.notOk(Object.prototype.toString.call([]) === '[object Number]');
assert.notOk(Object.prototype.toString.call({}) === '[object Number]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Number]');

assert.ok(Object.prototype.toString.call(42) === '[object Number]');
assert.ok(Object.prototype.toString.call(NaN) === '[object Number]');
assert.ok(Object.prototype.toString.call(Infinity) === '[object Number]');
assert.ok(Object.prototype.toString.call(new Number(42)) === '[object Number]');
19 changes: 19 additions & 0 deletions test/fixtures/is-number-object/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from 'assert';
import isNumber from 'is-number-object';

assert.notOk(isNumber(undefined));
assert.notOk(isNumber(null));
assert.notOk(isNumber(false));
assert.notOk(isNumber(true));
assert.notOk(isNumber('foo'));
assert.notOk(isNumber(function () {}));
assert.notOk(isNumber([]));
assert.notOk(isNumber({}));
assert.notOk(isNumber(/a/g));
assert.notOk(isNumber(new RegExp('a', 'g')));
assert.notOk(isNumber(new Date()));

assert.ok(isNumber(42));
assert.ok(isNumber(NaN));
assert.ok(isNumber(Infinity));
assert.ok(isNumber(new Number(42)));
18 changes: 18 additions & 0 deletions test/fixtures/is-number-object/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert';

assert.notOk(Object.prototype.toString.call(undefined) === '[object Number]');
assert.notOk(Object.prototype.toString.call(null) === '[object Number]');
assert.notOk(Object.prototype.toString.call(false) === '[object Number]');
assert.notOk(Object.prototype.toString.call(true) === '[object Number]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Number]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Number]');
assert.notOk(Object.prototype.toString.call([]) === '[object Number]');
assert.notOk(Object.prototype.toString.call({}) === '[object Number]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Number]');
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Number]');

assert.ok(Object.prototype.toString.call(42) === '[object Number]');
assert.ok(Object.prototype.toString.call(NaN) === '[object Number]');
assert.ok(Object.prototype.toString.call(Infinity) === '[object Number]');
assert.ok(Object.prototype.toString.call(new Number(42)) === '[object Number]');

0 comments on commit 39dd37c

Please sign in to comment.