Skip to content

Commit

Permalink
v0.1.12 :: Add equalIgnoreCase method to StringUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
telco2011 committed Jul 26, 2021
1 parent 4c3bd63 commit 40b93f2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yggdrasilts/volundr",
"version": "0.1.11",
"version": "0.1.12",
"description": "A set of utilities to do the developer live easy.",
"keywords": [
"typescript",
Expand Down
23 changes: 21 additions & 2 deletions src/boolean/BooleanUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BooleanUtils } from './BooleanUtils';
import BooleanUtils, { parseString } from './BooleanUtils';

import { StringUtils } from '../string/StringUtils';

describe('BooleanUtils', () => {
describe('parseString', () => {
describe('BooleanUtils.parseString', () => {
it('should be true for "true"', () => {
expect(BooleanUtils.parseString('true')).toBe(true);
});
Expand All @@ -19,4 +20,22 @@ describe('BooleanUtils', () => {
expect(BooleanUtils.parseString(StringUtils.EMPTY)).toBe(false);
});
});

describe('parseString func', () => {
it('should be true for "true"', () => {
expect(parseString('true')).toBe(true);
});
it('should be false for "false"', () => {
expect(parseString('false')).toBe(false);
});
it('should be false for "hello"', () => {
expect(parseString('hello')).toBe(false);
});
it('should be false for "5"', () => {
expect(parseString('5')).toBe(false);
});
it('should be false for EMPTY', () => {
expect(parseString(StringUtils.EMPTY)).toBe(false);
});
});
});
6 changes: 5 additions & 1 deletion src/boolean/BooleanUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @export
* @class BooleanUtils
*/
export class BooleanUtils {
export default class BooleanUtils {
/**
* Transform a string to boolean.
*
Expand All @@ -30,3 +30,7 @@ export class BooleanUtils {
}
}
}

const parseString = BooleanUtils.parseString;

export { parseString };
20 changes: 19 additions & 1 deletion src/string/StringUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StringUtils } from './StringUtils';
import Char from 'typescript-char';
import { StringUtils } from './StringUtils';

describe('StringUtils', () => {
describe('CONSTANTS', () => {
Expand Down Expand Up @@ -67,4 +67,22 @@ describe('StringUtils', () => {
);
});
});

describe('equalsIgnoreCase', () => {
it(`should be true: 'comparison' equalsIgnoreCase 'comparison'`, () => {
expect(StringUtils.equalIgnoreCase('comparison', 'comparison')).toBeTruthy();
});
it(`should be false: 'comparison' equalsIgnoreCase ''`, () => {
expect(StringUtils.equalIgnoreCase('comparison', '')).toBeFalsy()
});
it(`should be false: 'comparison' equalsIgnoreCase ' '`, () => {
expect(StringUtils.equalIgnoreCase('comparison', ' ')).toBeFalsy();
});
it(`should be false: 'comparison' equalsIgnoreCase null`, () => {
expect(StringUtils.equalIgnoreCase('comparison', null)).toBeFalsy();
});
it(`should be false: 'comparison' equalsIgnoreCase 'compare'`, () => {
expect(StringUtils.equalIgnoreCase('comparison', 'compare')).toBeFalsy();
});
});
});
21 changes: 21 additions & 0 deletions src/string/StringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as _ from 'lodash';

import Char from 'typescript-char';

/**
Expand Down Expand Up @@ -85,4 +86,24 @@ export class StringUtils {
): string {
return sourceText.replace(searchValue, replacement);
}

/**
* Compares two strings, returning true if they are equal, ignoring case.
*
* @static
* @param {string} str1 The first string.
* @param {string} str2 The second string.
* @returns true if the strings are equal (case-sensitive).
* @memberof StringUtils
*/
static equalIgnoreCase(str1: string, str2: string) {
if (!str1 || !str2) {
return false;
}
if ((typeof str1 !== 'string' && typeof str2 !== 'string') || (str1.length !== str2.length)) {
return false;
}

return str1.toLowerCase() === str2.toLowerCase();
}
}

0 comments on commit 40b93f2

Please sign in to comment.