2018-06-05 15:30:32 +00:00
|
|
|
import trimRight from 'vault/utils/trim-right';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Unit | Util | trim right', function () {
|
|
|
|
test('it trims extension array from end of string', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
const trimmedName = trimRight('my-file-name-is-cool.json', ['.json', '.txt', '.hcl', '.policy']);
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-file-name-is-cool');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it only trims extension array from the very end of string', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
const trimmedName = trimRight('my-file-name.json-is-cool.json', ['.json', '.txt', '.hcl', '.policy']);
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-file-name.json-is-cool');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns string as is if trim array is empty', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
const trimmedName = trimRight('my-file-name-is-cool.json', []);
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-file-name-is-cool.json');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns string as is if trim array is not passed in', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
const trimmedName = trimRight('my-file-name-is-cool.json');
|
2018-06-05 15:30:32 +00:00
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-file-name-is-cool.json');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2018-11-28 16:35:55 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it allows the last extension to also be part of the file name', function (assert) {
|
2018-11-28 16:35:55 +00:00
|
|
|
const trimmedName = trimRight('my-policy.hcl', ['.json', '.txt', '.hcl', '.policy']);
|
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-policy');
|
2018-11-28 16:35:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it allows the last extension to also be part of the file name and the extenstion', function (assert) {
|
2018-11-28 16:35:55 +00:00
|
|
|
const trimmedName = trimRight('my-policy.policy', ['.json', '.txt', '.hcl', '.policy']);
|
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-policy');
|
2018-11-28 16:35:55 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it passes endings into the regex unescaped when passing false as the third arg', function (assert) {
|
2018-11-28 16:35:55 +00:00
|
|
|
const trimmedName = trimRight('my-policypolicy', ['.json', '.txt', '.hcl', '.policy'], false);
|
|
|
|
|
|
|
|
// the . gets interpreted as regex wildcard so it also trims the y character
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(trimmedName, 'my-polic');
|
2018-11-28 16:35:55 +00:00
|
|
|
});
|
2018-06-14 04:06:19 +00:00
|
|
|
});
|