Skip to content

Commit 8029051

Browse files
author
Ovidiu Barabula
committed
feat(util): add get prefix utility function
1 parent 41b89bd commit 8029051

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/util/utility-functions.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ERRORS,
1111
flattenArray,
1212
getFnName,
13+
getPrefix,
1314
hasAllKeys,
1415
hasNested,
1516
limitFn,
@@ -508,4 +509,21 @@ describe('Utility Functions', () => {
508509
assert.throws(() => arrayOf([], false, true), ERRORS.ARRAYOF_NEEDS_STRINGS);
509510
});
510511
});
512+
513+
514+
describe('getPrefix()', () => {
515+
it('extracts prefix from prefixed string', () => {
516+
expect(getPrefix('foo:bar')).to.equal('foo');
517+
});
518+
519+
520+
it('extracts prefix from prefixed string with custom separator', () => {
521+
expect(getPrefix('foo|bar', '|')).to.equal('foo');
522+
});
523+
524+
525+
it('returns the original string if strings lacks separator', () => {
526+
expect(getPrefix('foo')).to.equal('foo');
527+
});
528+
});
511529
});

src/util/utility-functions.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ export function pluginPrefix(name: string): string {
306306

307307

308308
/**
309-
* Create plugin name
309+
* Create plugin name (e.g. 'foo' -> 'frontvue-plugin-foo')
310+
* @param name Plugin name
311+
* @param prefix Prefix to be added
310312
*/
311313
export function pluginName(name: string, prefix: string = 'frontvue'): string {
312314
if (name.indexOf('frontvue-plugin') === 0) {
@@ -317,6 +319,20 @@ export function pluginName(name: string, prefix: string = 'frontvue'): string {
317319
}
318320

319321

322+
/**
323+
* Extract prefix from string (e.g. 'foo:bar' -> 'foo')
324+
* @param string String to extract prefix from
325+
* @param separator Separator character
326+
*/
327+
export function getPrefix(string: string, separator: string = ':'): string {
328+
if (string.includes(separator)) {
329+
return string.split(separator)[0];
330+
}
331+
332+
return string;
333+
}
334+
335+
320336
/**
321337
* Require node module
322338
* Bypass webpack require replacement

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy