Skip to content

Commit

Permalink
ref: allow renames if content is shorter than the new string (it's ac…
Browse files Browse the repository at this point in the history
…tually possible, think about the scenario where the only content is the wikiref itself and the new name is longer than the old name...).
  • Loading branch information
manunamz committed Sep 26, 2023
1 parent 6f6a9d6 commit bf70379
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 44 deletions.
3 changes: 0 additions & 3 deletions src/lib/func/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export function renameFileName(
if (content.length < oldFileName.length) {
return 'wikirefs.renameFileName() error: content \'content\' is shorter than \'oldFileName\', aborting.';
}
if (content.length < newFileName.length) {
return 'wikirefs.renameFileName() error: content \'content\' is shorter than \'newFileName\', aborting.';
}
const wikiTextFilename: RegExp = new RegExp(RGX.GET.FILENAME, 'g');
return string.replace(wikiTextFilename, oldFileName, newFileName, content);
}
9 changes: 0 additions & 9 deletions src/lib/func/retype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export function retypeRefType(
if (content.length < oldRefType.length) {
return 'wikirefs.retypeRefType() error: content \'content\' is shorter than \'oldRefType\', aborting.';
}
if (content.length < newRefType.length) {
return 'wikirefs.retypeRefType() error: content \'content\' is shorter than \'newRefType\', aborting.';
}
content = retypeAttrType(oldRefType, newRefType, content);
content = retypeLinkType(oldRefType, newRefType, content);
return content;
Expand All @@ -26,9 +23,6 @@ export function retypeAttrType(
if (content.length < oldAttrType.length) {
return 'wikirefs.retypeAttrType() error: content \'content\' is shorter than \'oldAttrType\', aborting.';
}
if (content.length < newAttrType.length) {
return 'wikirefs.retypeAttrType() error: content \'content\' is shorter than \'newAttrType\', aborting.';
}
const wikiattr: RegExp = new RegExp(RGX.WIKI.ATTR, 'gm');
return string.replace(wikiattr, oldAttrType, newAttrType, content, { pad: true });
}
Expand All @@ -41,9 +35,6 @@ export function retypeLinkType(
if (content.length < oldLinkType.length) {
return 'wikirefs.retypeLinkType() error: content \'content\' is shorter than \'oldLinkType\', aborting.';
}
if (content.length < newLinkType.length) {
return 'wikirefs.retypeLinkType() error: content \'content\' is shorter than \'newLinkType\', aborting.';
}
const wikilink: RegExp = new RegExp(RGX.WIKI.LINK, 'g');
return string.replace(wikilink, oldLinkType, newLinkType, content);
}
9 changes: 1 addition & 8 deletions test/rename.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ describe('renameFileName()', () => {

describe('protect against mis-ordered function params', () => {

it('\'content\' string longer than \'oldFileName\' (protects against mis-ordered function params)', () => {
it('\'content\' string should be longer than \'oldFileName\'', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.renameFileName() error: content \'content\' is shorter than \'oldFileName\', aborting.';
const actlMkdn: string = wikirefs.renameFileName(mkdn, 'wikilink.pdf', 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

it('\'content\' string longer than \'newFileName\' (protects against mis-ordered function params)', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.renameFileName() error: content \'content\' is shorter than \'newFileName\', aborting.';
const actlMkdn: string = wikirefs.renameFileName('wikilink.pdf', mkdn, 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

});

});
Expand Down
27 changes: 3 additions & 24 deletions test/retype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ describe('retypeRefType()', () => {

describe('protect against mis-ordered function params', () => {

it('\'content\' string longer than \'oldRefType\' (protects against mis-ordered function params)', () => {
it('\'content\' string should be longer than \'oldRefType\'', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeRefType() error: content \'content\' is shorter than \'oldRefType\', aborting.';
const actlMkdn: string = wikirefs.retypeRefType(mkdn, 'wikilink.pdf', 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

it('\'content\' string longer than \'newRefType\' (protects against mis-ordered function params)', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeRefType() error: content \'content\' is shorter than \'newRefType\', aborting.';
const actlMkdn: string = wikirefs.retypeRefType('wikilink.pdf', mkdn, 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

});

});
Expand Down Expand Up @@ -131,20 +124,13 @@ describe('retypeAttrType()', () => {

describe('protect against mis-ordered function params', () => {

it('\'content\' string longer than \'oldAttrType\' (protects against mis-ordered function params)', () => {
it('\'content\' string should be longer than \'oldAttrType\'', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeAttrType() error: content \'content\' is shorter than \'oldAttrType\', aborting.';
const actlMkdn: string = wikirefs.retypeAttrType(mkdn, 'wikilink.pdf', 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

it('\'content\' string longer than \'newAttrType\' (protects against mis-ordered function params)', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeAttrType() error: content \'content\' is shorter than \'newAttrType\', aborting.';
const actlMkdn: string = wikirefs.retypeAttrType('wikilink.pdf', mkdn, 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

});

});
Expand Down Expand Up @@ -271,20 +257,13 @@ describe('retypeLinkType()', () => {

describe('protect against mis-ordered function params', () => {

it('\'content\' string longer than \'oldLinkType\'', () => {
it('\'content\' string should be longer than \'oldLinkType\'', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeLinkType() error: content \'content\' is shorter than \'oldLinkType\', aborting.';
const actlMkdn: string = wikirefs.retypeLinkType(mkdn, 'wikilink.pdf', 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

it('\'content\' string longer than \'oldLinkType\'', () => {
const mkdn: string = 'Here is some content with a [[wikiref]].';
const expdMkdn: string = 'wikirefs.retypeLinkType() error: content \'content\' is shorter than \'newLinkType\', aborting.';
const actlMkdn: string = wikirefs.retypeLinkType('wikilink.pdf', mkdn, 'hello-world.pdf');
assert.strictEqual(actlMkdn, expdMkdn);
});

});

});
Expand Down

0 comments on commit bf70379

Please sign in to comment.