From 1efd45737cf583b98f4133f1fcab24f4163b98bf Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Thu, 9 Jan 2025 16:39:12 -0700 Subject: [PATCH 1/6] string-to-camel helper --- ui/app/helpers/string-to-camel.js | 37 ++++++++++++ .../helpers/string-to-camel-test.js | 60 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 ui/app/helpers/string-to-camel.js create mode 100644 ui/tests/integration/helpers/string-to-camel-test.js diff --git a/ui/app/helpers/string-to-camel.js b/ui/app/helpers/string-to-camel.js new file mode 100644 index 000000000000..da2ca29a151b --- /dev/null +++ b/ui/app/helpers/string-to-camel.js @@ -0,0 +1,37 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: BUSL-1.1 + */ + +import { helper as buildHelper } from '@ember/component/helper'; +import { assert } from '@ember/debug'; + +// This helper turns strings with spaces into camelCase strings, example: 'hello world' -> 'helloWorld' +// If an array of strings is passed, this helper returns an array of camelCase strings. +// Does not handle accented characters +export function stringToCamelCase(str) { + if (!str) return; + if (Array.isArray(str)) { + return str.map((s) => { + assert(`must pass in a string or array of strings`, typeof s === 'string'); + // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of + s = s.toLowerCase(); + return s + .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { + return index === 0 ? word.toLowerCase() : word.toUpperCase(); + }) + .replace(/\s+/g, ''); + }); + } else { + // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of + assert(`must pass in a string or array of strings`, typeof str === 'string'); + str = str.toLowerCase(); + return str + .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { + return index === 0 ? word.toLowerCase() : word.toUpperCase(); + }) + .replace(/\s+/g, ''); + } +} + +export default buildHelper(stringToCamelCase); diff --git a/ui/tests/integration/helpers/string-to-camel-test.js b/ui/tests/integration/helpers/string-to-camel-test.js new file mode 100644 index 000000000000..16e6a5dc925c --- /dev/null +++ b/ui/tests/integration/helpers/string-to-camel-test.js @@ -0,0 +1,60 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: BUSL-1.1 + */ + +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'vault/tests/helpers'; +import { stringToCamelCase } from 'vault/helpers/string-to-camel'; + +module('Integration | Helper | string-to-camel', function (hooks) { + setupRenderingTest(hooks); + + test('it returns camelCase string with all caps and two words separated by space', async function (assert) { + const string = 'FOO Bar'; + const expected = 'fooBar'; + const result = stringToCamelCase(string); + + assert.strictEqual( + result, + expected, + 'camelCase string returned for call caps and two words separated by space' + ); + }); + + test('it returns an array of camelCased strings if an array of strings passed in', function (assert) { + const string = ['FOO Bar', 'Baz Qux', 'wibble wobble', 'wobble WIBBLes']; + const expected = ['fooBar', 'bazQux', 'wibbleWobble', 'wobbleWibbles']; + const result = stringToCamelCase(string); + assert.deepEqual(result, expected, 'camelCase array of strings returned for all sorts of strings'); + }); + + test('it returns camelCased string if string contains numbers', function (assert) { + const string = '123'; + const expected = '123'; + const result = stringToCamelCase(string); + assert.strictEqual(result, expected, 'camelCase kind of handles strings with numbers'); + }); + + test('it returns error if str is not a string', function (assert) { + const string = { name: 'foo.bar*baz' }; + let result; + try { + result = stringToCamelCase(string); + } catch (e) { + result = e.message; + } + assert.deepEqual(result, 'Assertion Failed: must pass in a string or array of strings'); + }); + + test('it returns error if str is not an array of string', function (assert) { + const string = [{ name: 'foo.bar*baz' }]; + let result; + try { + result = stringToCamelCase(string); + } catch (e) { + result = e.message; + } + assert.deepEqual(result, 'Assertion Failed: must pass in a string or array of strings'); + }); +}); From dcea9ff02c958d7e3d180478e8ebd82228a3892f Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Thu, 9 Jan 2025 16:45:03 -0700 Subject: [PATCH 2/6] fix: --- ui/app/helpers/string-to-camel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/app/helpers/string-to-camel.js b/ui/app/helpers/string-to-camel.js index da2ca29a151b..45051eea63ba 100644 --- a/ui/app/helpers/string-to-camel.js +++ b/ui/app/helpers/string-to-camel.js @@ -14,7 +14,7 @@ export function stringToCamelCase(str) { if (Array.isArray(str)) { return str.map((s) => { assert(`must pass in a string or array of strings`, typeof s === 'string'); - // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of + // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of iAMEndpoint s = s.toLowerCase(); return s .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { @@ -23,7 +23,7 @@ export function stringToCamelCase(str) { .replace(/\s+/g, ''); }); } else { - // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of + // lower case the entire string to handle situations like IAM Endpoint -> iamEndpoint instead of iAMEndpoint assert(`must pass in a string or array of strings`, typeof str === 'string'); str = str.toLowerCase(); return str From dc3ad2cbdcb0520de7fbf5a06c1ea6672a8596d7 Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Thu, 9 Jan 2025 16:47:13 -0700 Subject: [PATCH 3/6] Update string-to-camel-test.js --- ui/tests/integration/helpers/string-to-camel-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/tests/integration/helpers/string-to-camel-test.js b/ui/tests/integration/helpers/string-to-camel-test.js index 16e6a5dc925c..8e01d17b0196 100644 --- a/ui/tests/integration/helpers/string-to-camel-test.js +++ b/ui/tests/integration/helpers/string-to-camel-test.js @@ -29,7 +29,7 @@ module('Integration | Helper | string-to-camel', function (hooks) { assert.deepEqual(result, expected, 'camelCase array of strings returned for all sorts of strings'); }); - test('it returns camelCased string if string contains numbers', function (assert) { + test('it returns string if string is numbers', function (assert) { const string = '123'; const expected = '123'; const result = stringToCamelCase(string); From 6bc001ffc4365e9e05a57dbfb6da937aecf69a24 Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Thu, 9 Jan 2025 17:40:48 -0700 Subject: [PATCH 4/6] update comment --- ui/app/helpers/string-to-camel.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ui/app/helpers/string-to-camel.js b/ui/app/helpers/string-to-camel.js index 45051eea63ba..3dd62be4a7a5 100644 --- a/ui/app/helpers/string-to-camel.js +++ b/ui/app/helpers/string-to-camel.js @@ -6,8 +6,9 @@ import { helper as buildHelper } from '@ember/component/helper'; import { assert } from '@ember/debug'; -// This helper turns strings with spaces into camelCase strings, example: 'hello world' -> 'helloWorld' -// If an array of strings is passed, this helper returns an array of camelCase strings. +// This helper is simlar to the Ember string camelize helper but it does some additional handling: +// it allows you to pass in an array of strings +// it lowercases the entire string before converting to camelCase preventing situations like IAM Endpoint -> iamEndpoint instead of iAMEndpoint // Does not handle accented characters export function stringToCamelCase(str) { if (!str) return; From 81b1c4c5644fd83e2e862d22261d783edd2942d9 Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Fri, 10 Jan 2025 08:53:51 -0700 Subject: [PATCH 5/6] rename and clarify comment --- .../{string-to-camel.js => string-array-to-camel.js} | 8 ++++---- ...ing-to-camel-test.js => string-array-to-camel-test.js} | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) rename ui/app/helpers/{string-to-camel.js => string-array-to-camel.js} (82%) rename ui/tests/integration/helpers/{string-to-camel-test.js => string-array-to-camel-test.js} (96%) diff --git a/ui/app/helpers/string-to-camel.js b/ui/app/helpers/string-array-to-camel.js similarity index 82% rename from ui/app/helpers/string-to-camel.js rename to ui/app/helpers/string-array-to-camel.js index 3dd62be4a7a5..1e75d4f29dd9 100644 --- a/ui/app/helpers/string-to-camel.js +++ b/ui/app/helpers/string-array-to-camel.js @@ -6,11 +6,11 @@ import { helper as buildHelper } from '@ember/component/helper'; import { assert } from '@ember/debug'; -// This helper is simlar to the Ember string camelize helper but it does some additional handling: +// This helper is similar to the Ember string camelize helper but it does some additional handling: // it allows you to pass in an array of strings // it lowercases the entire string before converting to camelCase preventing situations like IAM Endpoint -> iamEndpoint instead of iAMEndpoint -// Does not handle accented characters -export function stringToCamelCase(str) { +// it does not handle accented characters so try not use for user inputted strings. +export function stringArrayToCamelCase(str) { if (!str) return; if (Array.isArray(str)) { return str.map((s) => { @@ -35,4 +35,4 @@ export function stringToCamelCase(str) { } } -export default buildHelper(stringToCamelCase); +export default buildHelper(stringArrayToCamelCase); diff --git a/ui/tests/integration/helpers/string-to-camel-test.js b/ui/tests/integration/helpers/string-array-to-camel-test.js similarity index 96% rename from ui/tests/integration/helpers/string-to-camel-test.js rename to ui/tests/integration/helpers/string-array-to-camel-test.js index 8e01d17b0196..7746b6602740 100644 --- a/ui/tests/integration/helpers/string-to-camel-test.js +++ b/ui/tests/integration/helpers/string-array-to-camel-test.js @@ -5,7 +5,7 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'vault/tests/helpers'; -import { stringToCamelCase } from 'vault/helpers/string-to-camel'; +import { stringToCamelCase } from 'vault/helpers/string-array-to-camel'; module('Integration | Helper | string-to-camel', function (hooks) { setupRenderingTest(hooks); From 20e772bf9623003529e69ee073e778fc253d937b Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Fri, 10 Jan 2025 09:16:14 -0700 Subject: [PATCH 6/6] welp, forgot to update test --- .../helpers/string-array-to-camel-test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ui/tests/integration/helpers/string-array-to-camel-test.js b/ui/tests/integration/helpers/string-array-to-camel-test.js index 7746b6602740..be71685d6f52 100644 --- a/ui/tests/integration/helpers/string-array-to-camel-test.js +++ b/ui/tests/integration/helpers/string-array-to-camel-test.js @@ -5,15 +5,15 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'vault/tests/helpers'; -import { stringToCamelCase } from 'vault/helpers/string-array-to-camel'; +import { stringArrayToCamelCase } from 'vault/helpers/string-array-to-camel'; -module('Integration | Helper | string-to-camel', function (hooks) { +module('Integration | Helper | string-array-to-camel', function (hooks) { setupRenderingTest(hooks); test('it returns camelCase string with all caps and two words separated by space', async function (assert) { const string = 'FOO Bar'; const expected = 'fooBar'; - const result = stringToCamelCase(string); + const result = stringArrayToCamelCase(string); assert.strictEqual( result, @@ -25,14 +25,14 @@ module('Integration | Helper | string-to-camel', function (hooks) { test('it returns an array of camelCased strings if an array of strings passed in', function (assert) { const string = ['FOO Bar', 'Baz Qux', 'wibble wobble', 'wobble WIBBLes']; const expected = ['fooBar', 'bazQux', 'wibbleWobble', 'wobbleWibbles']; - const result = stringToCamelCase(string); + const result = stringArrayToCamelCase(string); assert.deepEqual(result, expected, 'camelCase array of strings returned for all sorts of strings'); }); test('it returns string if string is numbers', function (assert) { const string = '123'; const expected = '123'; - const result = stringToCamelCase(string); + const result = stringArrayToCamelCase(string); assert.strictEqual(result, expected, 'camelCase kind of handles strings with numbers'); }); @@ -40,7 +40,7 @@ module('Integration | Helper | string-to-camel', function (hooks) { const string = { name: 'foo.bar*baz' }; let result; try { - result = stringToCamelCase(string); + result = stringArrayToCamelCase(string); } catch (e) { result = e.message; } @@ -51,7 +51,7 @@ module('Integration | Helper | string-to-camel', function (hooks) { const string = [{ name: 'foo.bar*baz' }]; let result; try { - result = stringToCamelCase(string); + result = stringArrayToCamelCase(string); } catch (e) { result = e.message; }