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

greatly simplify snakeCaseWords #3881

Merged
merged 5 commits into from
Oct 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions apps/dashboard/app/javascript/dynamic_forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,13 @@ function mountainCaseWords(str) {
function snakeCaseWords(str) {
if(str === undefined) return undefined;

let snakeCase = "";

str.split('').forEach((c, index) => {
if(c === '-' || c === '_') {
snakeCase += '_';
} else if (index == 0) {
snakeCase += c.toLowerCase();
} else if(c == c.toUpperCase() && isNaN(c)) {
const nextIsUpper = (index + 1 !== str.length) ? str[index + 1] === str[index + 1].toUpperCase() : true;
const nextIsNum = !isNaN(str[index + 1]);
if ((str[index-1] === '_' || nextIsUpper) && !nextIsNum) {
snakeCase += c.toLowerCase();
} else {
snakeCase += `_${c.toLowerCase()}`;
}
} else {
snakeCase += c;
}
});
// find all the captial case words and if none are found, we'll just bascially
// return the same string.
const rex = /([A-Z]{1}[a-z]*[0-9]*)|.+/g;
const words = str.match(rex);

return snakeCase;
// filter out emtpy matches to avoid having a _ at the end.
return words.filter(word => word != '').map(word => word.toLowerCase()).join('_');
}

/**
Expand Down
29 changes: 29 additions & 0 deletions apps/dashboard/test/system/batch_connect_widgets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,33 @@ def make_bc_app(dir, form)
refute(find("##{bc_ele_id('eastern_city')}", visible: false).visible?)
end
end

test 'weird ids like aa_b_cc work' do
Dir.mktmpdir do |dir|
form = <<~HEREDOC
form:
- aa
- aa_b_cc
attributes:
aa:
widget: select
options:
- [ "foo", "foo", data-hide-aa-b-cc: true]
- ['bar', 'bar']
aa_b_cc:
widget: text_field
HEREDOC

make_bc_app(dir, form)
visit new_batch_connect_session_context_url('sys/app')

# foo is default, so aa_b_cc should be hidden
assert('foo', find("##{bc_ele_id('aa')}").value)
refute(find("##{bc_ele_id('aa_b_cc')}", visible: false).visible?)

# select bar, and now aa_b_cc is available.
select('bar', from: bc_ele_id('aa'))
assert(find("##{bc_ele_id('aa_b_cc')}").visible?)
end
end
end
Loading