Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(deps): inline decamelize dependency #1099

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"url": "https://github.com/salsita/node-pg-migrate.git"
},
"dependencies": {
"decamelize": "^5.0.0",
"mkdirp": "~1.0.0",
"yargs": "~17.7.0"
},
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

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

5 changes: 3 additions & 2 deletions src/utils/createSchemalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import decamelize from 'decamelize';
import { identity, quote } from '.';
import type { Name } from '../operations/generalTypes';
import { decamelize } from './decamelize';
import { identity } from './identity';
import { quote } from './quote';

/** @deprecated Use createSchemalize(options) instead. */
export function createSchemalize(
Expand Down
36 changes: 36 additions & 0 deletions src/utils/decamelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Simplified copy of https://github.com/sindresorhus/decamelize/blob/5deb8c5b88c3dbc93ef0d68faa0fc45cf98cad9d/index.js
const REPLACEMENT = '$1_$2';

/**
* Convert a string into a decamelize lowercased one.
*
* @example
* unicornsAndRainbows → unicorns_and_rainbows
* myURLString → my_url_string
*
* @param text The string to decamelize.
* @returns The decamelized string.
*/
export function decamelize(text: string): string {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
// Checking the second character is done later on. Therefore process shorter strings here.
if (text.length < 2) {
return text.toLowerCase();
}

// Split lowercase sequences followed by uppercase character.
// `dataForUSACounties` → `data_For_USACounties`
// `myURLstring → `my_URLstring`
const decamelized = text.replace(
/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu,
REPLACEMENT
);

// Split multiple uppercase characters followed by one or more lowercase characters.
// `my_URLstring` → `my_ur_lstring`
return decamelized
.replace(
/(\p{Uppercase_Letter})(\p{Uppercase_Letter}\p{Lowercase_Letter}+)/gu,
REPLACEMENT
)
.toLowerCase();
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { createSchemalize } from './createSchemalize';
export { createTransformer } from './createTransformer';
export { decamelize } from './decamelize';
export { escapeValue } from './escapeValue';
export { formatLines } from './formatLines';
export { formatParams } from './formatParams';
Expand Down
30 changes: 30 additions & 0 deletions test/utils/decamelize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest';
import { decamelize } from '../../src/utils';

describe('utils', () => {
describe('decamelize', () => {
it.each([
['', ''],
['A', 'a'],
['A B', 'a b'],
['a2b', 'a2b'],
['A2B', 'a2_b'],
['_A2B', '_a2_b'],
['myURLstring', 'my_ur_lstring'],
['unicornsAndRainbows', 'unicorns_and_rainbows'],
['UNICORNS AND RAINBOWS', 'unicorns and rainbows'],
['unicorns-and-rainbows', 'unicorns-and-rainbows'],
['thisIsATest', 'this_is_a_test'],
['myURLString', 'my_url_string'],
['URLString', 'url_string'],
['StringURL', 'string_url'],
['testGUILabel', 'test_gui_label'],
['CAPLOCKED1', 'caplocked1'],
['my_URL_string', 'my_url_string'],
])('should handle string %s', (input, expected) => {
const actual = decamelize(input);

expect(actual).toBe(expected);
});
});
});