Add dedent utility
ADD
packages/@romejs/string-utils/dedent.test.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {dedent} from './dedent';
+import {test} from 'rome';
+
+test(
+ 'dedent',
+ (t) => {
+ t.is(dedent('\tx\n\ty'), 'x\ny');
+
+ t.is(dedent('\tx\n\t\ty\n\tz'), 'x\n\ty\nz');
+
+ t.is(dedent`
+ if (x) {
+ y = ${'1'};
+ }
+ `, 'if (x) {\n y = 1;\n}');
+ },
+);

ADD
packages/@romejs/string-utils/dedent.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+export function dedent(
+ literals: string | TemplateStringsArray,
+ ...values: Array<string>
+): string {
+ let string: string = '';
+
+ if (typeof literals === 'string') {
+ string = literals;
+ } else {
+ const parts: Array<string> = [];
+
+ // Perform the interpolation
+ for (let i = 0; i < literals.raw.length; i++) {
+ parts.push(literals.raw[i]);
+ if (i < values.length) {
+ parts.push(values[i]);
+ }
+ }
+
+ string = parts.join('');
+ }
+
+ // Find min indentation
+ const match = string.match(/^[ \t]*(?=\S)/gm);
+ if (match === null) {
+ return string;
+ }
+
+ const indent = Math.min(...match.map((x) => x.length));
+
+ // Remove indentation
+ return string.replace(new RegExp(`^[ \\t]{${indent}}`, 'gm'), '').trim();
+}

MODIFY
packages/@romejs/string-utils/index.ts
packages/@romejs/string-utils/index.ts
* LICENSE file in the root directory of this source tree.
*/
-export * from './orderBySimilarity';
-
-export * from './naturalCompare';
-
-export * from './escapeSplit';
-
-export * from './humanizeFileSize';
-
-export * from './humanizeNumber';
-
-export * from './isEscaped';
-
-export * from './humanizeTime';
-
-export * from './removeSuffix';
-
-export * from './removePrefix';
-
-export * from './findClosestStringMatch';
-
-export * from './orderByNatural';
-
-export * from './toKebabCase';
-
-export * from './toCamelCase';
-
-export * from './removeCarriageReturn';
-
-export * from './preserveCasing';
+export {dedent} from './dedent';
+export {escapeSplit} from './escapeSplit';
+export {findClosestStringMatch} from './findClosestStringMatch';
+export {humanizeFileSize} from './humanizeFileSize';
+export {humanizeNumber} from './humanizeNumber';
+export {humanizeTime} from './humanizeTime';
+export {isEscaped} from './isEscaped';
+export {naturalCompare} from './naturalCompare';
+export {orderByNatural} from './orderByNatural';
+export {orderBySimilarity} from './orderBySimilarity';
+export {preserveCasing} from './preserveCasing';
+export {removeCarriageReturn} from './removeCarriageReturn';
+export {removePrefix} from './removePrefix';
+export {removeSuffix} from './removeSuffix';
+export {toCamelCase} from './toCamelCase';
+export {toKebabCase} from './toKebabCase';

Add dedent utility