Skip to content

Commit

Permalink
feat: add pad-left
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanru committed Jul 21, 2024
1 parent fdb7ad8 commit 1ae1aa5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ import errorCause from './error-cause/index.js';

import leftPad from './left-pad/index.js';

import padLeft from './pad-left/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand Down Expand Up @@ -224,4 +226,5 @@ export const codemods = {
'last-index-of': lastIndexOf,
'error-cause': errorCause,
'left-pad': leftPad,
'pad-left': padLeft,
};
49 changes: 49 additions & 0 deletions codemods/pad-left/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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: 'pad-left',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport('pad-left', root, j);
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.replaceWith(({ node }) => {
const [stringArg, ...otherArgs] = node.arguments;
return j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
// @ts-ignore
j.parenthesizedExpression(stringArg),
j.identifier('toString'),
),
[],
),
j.identifier('padStart'),
),
[...otherArgs],
);
});

return root.toSource(options);
},
};
}
9 changes: 9 additions & 0 deletions test/fixtures/pad-left/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var assert = require("assert");

assert.equal(("foo").toString().padStart(5), "foo ");

assert.equal(("foobar").toString().padStart(6), "foobar");

assert.equal((1).toString().padStart(2, "0"), "01");

assert.equal((17).toString().padStart(5, 0), "00017");
10 changes: 10 additions & 0 deletions test/fixtures/pad-left/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var leftPad = require("pad-left");
var assert = require("assert");

assert.equal(leftPad("foo", 5), "foo ");

assert.equal(leftPad("foobar", 6), "foobar");

assert.equal(leftPad(1, 2, "0"), "01");

assert.equal(leftPad(17, 5, 0), "00017");
9 changes: 9 additions & 0 deletions test/fixtures/pad-left/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var assert = require("assert");

assert.equal(("foo").toString().padStart(5), "foo ");

assert.equal(("foobar").toString().padStart(6), "foobar");

assert.equal((1).toString().padStart(2, "0"), "01");

assert.equal((17).toString().padStart(5, 0), "00017");

0 comments on commit 1ae1aa5

Please sign in to comment.