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

improve getInitials function with more case #303

Merged
merged 4 commits into from
Aug 23, 2023
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
42 changes: 33 additions & 9 deletions libs/utils/dist/cjs/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { FileType } from './types/files';
import { IKeyValue } from './types/helpers';
import {FileType} from './types/files';
import {IKeyValue} from './types/helpers';
export declare const getGraphqlTypeFromLibraryName: (library: string) => string;
export declare const getGraphqlQueryNameFromLibraryName: (library: string) => string;
export declare const isFileAllowed: (fsPath: string, allowList: string[], ignoreList: string[], filePath: string) => boolean;
export declare const localizedTranslation: (translations: Record<string, string>, availableLanguages: string[]) => string;
export declare const isFileAllowed: (
fsPath: string,
allowList: string[],
ignoreList: string[],
filePath: string
) => boolean;
export declare const localizedTranslation: (
translations: Record<string, string>,
availableLanguages: string[]
) => string;
/**
*
* @param str
* @param format 'hsl' || 'rgb' || 'hex'
* @param saturation in percent, default to 30
* @param luminosity in percent, default to 80
*/
export declare const stringToColor: (str?: string | null, format?: string, saturation?: number, luminosity?: number) => string;
export declare const stringToColor: (
str?: string | null,
format?: string,
saturation?: number,
luminosity?: number
) => string;
export declare const getInvertColor: (color: string) => string;
/**
* Parse string to extract args.
Expand All @@ -21,17 +34,27 @@ export declare const getInvertColor: (color: string) => string;
* eg. "-library product -type link" => {library: product, type: link}
* @param mapping
*/
export declare const extractArgsFromString: (mapping: string) => {
export declare const extractArgsFromString: (
mapping: string
) => {
[arg: string]: string;
};
export declare const objectToNameValueArray: <T>(obj: IKeyValue<T>) => {
export declare const objectToNameValueArray: <T>(
obj: IKeyValue<T>
) => {
name: string;
value: T;
}[];
export declare const nameValArrayToObj: (arr?: Array<{}>, keyFieldName?: string, valueFieldName?: string) => {
export declare const nameValArrayToObj: (
arr?: Array<{}>,
keyFieldName?: string,
valueFieldName?: string
) => {
[key: string]: any;
};
export declare const getLibraryGraphqlNames: (libraryId: string) => {
export declare const getLibraryGraphqlNames: (
libraryId: string
) => {
query: string;
type: string;
list: string;
Expand All @@ -46,6 +69,7 @@ export declare const getFileType: (fileName: string) => FileType;
*/
export declare const getCallStack: (depth?: number) => string[];
export declare const getInitials: (label: string, length?: number) => string;
export declare const _getInitialEngine: (words: string[], length: number) => string;
/**
* Format an ID: remove accents, any special characters, replace spaces by underscore and make sure there is no double underscore
*
Expand Down
36 changes: 31 additions & 5 deletions libs/utils/dist/cjs/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/utils/dist/cjs/utils.js.map

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions libs/utils/dist/esm/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { FileType } from './types/files';
import { IKeyValue } from './types/helpers';
import {FileType} from './types/files';
import {IKeyValue} from './types/helpers';
export declare const getGraphqlTypeFromLibraryName: (library: string) => string;
export declare const getGraphqlQueryNameFromLibraryName: (library: string) => string;
export declare const isFileAllowed: (fsPath: string, allowList: string[], ignoreList: string[], filePath: string) => boolean;
export declare const localizedTranslation: (translations: Record<string, string>, availableLanguages: string[]) => string;
export declare const isFileAllowed: (
fsPath: string,
allowList: string[],
ignoreList: string[],
filePath: string
) => boolean;
export declare const localizedTranslation: (
translations: Record<string, string>,
availableLanguages: string[]
) => string;
/**
*
* @param str
* @param format 'hsl' || 'rgb' || 'hex'
* @param saturation in percent, default to 30
* @param luminosity in percent, default to 80
*/
export declare const stringToColor: (str?: string | null, format?: string, saturation?: number, luminosity?: number) => string;
export declare const stringToColor: (
str?: string | null,
format?: string,
saturation?: number,
luminosity?: number
) => string;
export declare const getInvertColor: (color: string) => string;
/**
* Parse string to extract args.
Expand All @@ -21,17 +34,27 @@ export declare const getInvertColor: (color: string) => string;
* eg. "-library product -type link" => {library: product, type: link}
* @param mapping
*/
export declare const extractArgsFromString: (mapping: string) => {
export declare const extractArgsFromString: (
mapping: string
) => {
[arg: string]: string;
};
export declare const objectToNameValueArray: <T>(obj: IKeyValue<T>) => {
export declare const objectToNameValueArray: <T>(
obj: IKeyValue<T>
) => {
name: string;
value: T;
}[];
export declare const nameValArrayToObj: (arr?: Array<{}>, keyFieldName?: string, valueFieldName?: string) => {
export declare const nameValArrayToObj: (
arr?: Array<{}>,
keyFieldName?: string,
valueFieldName?: string
) => {
[key: string]: any;
};
export declare const getLibraryGraphqlNames: (libraryId: string) => {
export declare const getLibraryGraphqlNames: (
libraryId: string
) => {
query: string;
type: string;
list: string;
Expand All @@ -46,6 +69,7 @@ export declare const getFileType: (fileName: string) => FileType;
*/
export declare const getCallStack: (depth?: number) => string[];
export declare const getInitials: (label: string, length?: number) => string;
export declare const _getInitialEngine: (words: string[], length: number) => string;
/**
* Format an ID: remove accents, any special characters, replace spaces by underscore and make sure there is no double underscore
*
Expand Down
33 changes: 29 additions & 4 deletions libs/utils/dist/esm/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/utils/dist/esm/utils.js.map

Large diffs are not rendered by default.

26 changes: 23 additions & 3 deletions libs/utils/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,29 @@ describe('utils', () => {

describe('getInitials', () => {
test('Return label initials for given length', async () => {
expect(getInitials('Dwight Schrute', 2)).toBe('DS');
expect(getInitials('Dwight Schrute', 1)).toBe('D');
expect(getInitials('Dwight', 2)).toBe('DW');
expect(getInitials('', 2)).toBe('?');
expect(getInitials(' ', 2)).toBe('?');
expect(getInitials('Foo Bar', 0)).toBe('?');
expect(getInitials('Foo Bar', -1)).toBe('?');
expect(getInitials('Foo', 1)).toBe('F');
expect(getInitials('Foo', 2)).toBe('FO');
expect(getInitials('Foo', 4)).toBe('FOO');
expect(getInitials('Foo Bar', 1)).toBe('F');
expect(getInitials('Foo Bar', 2)).toBe('FB');
expect(getInitials('Foo - Bar', 2)).toBe('FB');
expect(getInitials('Foo & Bar', 2)).toBe('FB');
expect(getInitials('- Foo - Bar', 2)).toBe('FB');
expect(getInitials('- Foo -', 2)).toBe('FO');
expect(getInitials('123 Foo', 2)).toBe('FO');
expect(getInitials('132 - Foo Bar', 2)).toBe('FB');
expect(getInitials('987 & 654 -23_AB', 2)).toBe('AB');
expect(getInitials('987654', 2)).toBe('98');
expect(getInitials('987 - 654', 2)).toBe('96');
expect(getInitials('987 - aaaa', 4)).toBe('AAAA');
expect(getInitials('^ - ^456789', 6)).toBe('456789');
expect(getInitials('^ - ^456789', 7)).toBe('456789');
expect(getInitials('987 - 654', 1)).toBe('9');
expect(getInitials('^ - ^', 2)).toBe('^-');
});
});

Expand Down
35 changes: 29 additions & 6 deletions libs/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import minimatch from 'minimatch';
import * as extensions from './MIMEByExtension.json';
import {FileType} from './types/files';
import {IKeyValue} from './types/helpers';
import isEmpty from 'lodash/isEmpty';

export const getGraphqlTypeFromLibraryName = (library: string): string => {
return flow([camelCase, upperFirst, trimEnd, partialRight(trimEnd, 's')])(library);
Expand Down Expand Up @@ -200,16 +201,38 @@ export const getCallStack = (depth: number = 2): string[] => {
};

export const getInitials = (label: string, length: number = 2) => {
if (typeof label !== 'string') {
if (typeof label !== 'string' || isEmpty(label.trim()) || length < 1) {
return '?';
}

const words = label.split(' ').slice(0, length);
const letters = words.length >= length ? words.map(word => word[0]).join('') : words[0].slice(0, length);

return letters.toUpperCase();
const words: string[] = label.split(' ');
/* Setting up the list of word by using a regex according to the label sent
if the label contains letters & numbers, filter only on these letters
if the label contains only numbers, filter only on these numbers
symbols are ignored by the regex
if both list filtered by the regex are null, using the basic filter and split the label by space
*/
const letterRegex = new RegExp(/[A-Za-z]+/g);
const numberRegex = new RegExp(/[1-9]+/g);
const wordsRegex = label.match(letterRegex) ? label.match(letterRegex) : label.match(numberRegex);
return wordsRegex !== null ? _getInitialEngine(wordsRegex, length) : _getInitialEngine(words, length);
};

export const _getInitialEngine = (words: string[], length: number) => {
let initials: string = '';
if (words.length === 1) {
initials = words[0].slice(0, length);
} else {
//the number of initial to display cannot exceed the number of words filtered
if (words.length < length) {
length = words.length;
}
Kekelgrs marked this conversation as resolved.
Show resolved Hide resolved
for (let index = 0; index < length; index++) {
initials = initials + words[index].charAt(0);
}
}
return initials.toUpperCase();
};

/**
* Format an ID: remove accents, any special characters, replace spaces by underscore and make sure there is no double underscore
*
Expand Down
Loading