Skip to content

Commit

Permalink
Move checks to conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
samirsilwal committed Oct 6, 2024
1 parent 915e2e9 commit d7342df
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 115 deletions.
76 changes: 38 additions & 38 deletions dist/h.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ exports.and = and;
exports.or = or;
exports.coalesce = coalesce;
exports.includes = includes;
exports.startsWith = startsWith;
exports.endsWith = endsWith;

var _utils = require('../util/utils');

Expand Down Expand Up @@ -430,6 +432,42 @@ function includes(array, value) {

return false;
}

/**
* Check if a string starts with a given substring.
*
* @example
* {{startsWith 'Just Wow' 'Just'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
function startsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.startsWith(searchString);
}

/**
* Check if a string ends with a given substring.
*
* @example
* {{endsWith 'Just Wow' 'Wow'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
function endsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.endsWith(searchString);
}
},{"../util/utils":9}],4:[function(require,module,exports){
(function (global){(function (){
'use strict';
Expand Down Expand Up @@ -808,8 +846,6 @@ exports.first = first;
exports.last = last;
exports.concat = concat;
exports.join = join;
exports.startsWith = startsWith;
exports.endsWith = endsWith;
exports.unique = unique;
exports.trim = trim;

Expand Down Expand Up @@ -1059,42 +1095,6 @@ function join(params, delimiter) {
return params.join(delimiter);
}

/**
* Check if a string starts with a given substring.
*
* @example
* {{startsWith 'Just Wow' 'Just'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
function startsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.startsWith(searchString);
}

/**
* Check if a string ends with a given substring.
*
* @example
* {{endsWith 'Just Wow' 'Wow'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
function endsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.endsWith(searchString);
}

/**
* Extract unique elements from given collection.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/h.min.js

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/helpers/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,40 @@ export function includes(array, value, strict = true) {

return false;
}


/**
* Check if a string starts with a given substring.
*
* @example
* {{startsWith 'Just Wow' 'Just'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
export function startsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.startsWith(searchString);
}

/**
* Check if a string ends with a given substring.
*
* @example
* {{endsWith 'Just Wow' 'Wow'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
export function endsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.endsWith(searchString);
}
36 changes: 0 additions & 36 deletions src/helpers/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,42 +234,6 @@ export function join(params, delimiter) {
return params.join(delimiter);
}

/**
* Check if a string starts with a given substring.
*
* @example
* {{startsWith 'Just Wow' 'Just'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
export function startsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.startsWith(searchString);
}

/**
* Check if a string ends with a given substring.
*
* @example
* {{endsWith 'Just Wow' 'Wow'}} => true
*
* @param {string} str
* @param {string} searchString
* @returns {boolean}
*/
export function endsWith(str, searchString) {
if (typeof searchString !== 'string' || typeof str !== 'string') {
return false;
}

return str.endsWith(searchString);
}

/**
* Extract unique elements from given collection.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/helpers/conditionals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,44 @@ describe('conditionals', () => {
expect(template({ array: array, value: value })).toEqual('false');
});
});

describe('startsWith', () => {
it('should return true if the string starts with the provided substring', () => {
expect(conditionals.startsWith('Hello World!', 'Hello')).toEqual(true);
});

it('should return false if the string does not start with the provided substring', () => {
expect(conditionals.startsWith('Hello World!', 'World')).toEqual(false);
});

it('should work as expected after compilation (Basic Support)', () => {
const template = compile('{{startsWith string subString}}');
const obj = {
string: 'Hello World!',
subString: 'Hello'
};

expect(JSON.parse(template(obj))).toEqual(true);
});
});

describe('endsWith', () => {
it('should return true if the string ends with the provided substring', () => {
expect(conditionals.endsWith('Hello World!', 'World!')).toEqual(true);
});

it('should return false if the string does not end with the provided substring', () => {
expect(conditionals.endsWith('Hello World!', 'Hello')).toEqual(false);
});

it('should work as expected after compilation (Basic Support)', () => {
const template = compile('{{endsWith string subString}}');
const obj = {
string: 'Hello World!',
subString: 'World!'
};

expect(JSON.parse(template(obj))).toEqual(true);
});
});
});
40 changes: 0 additions & 40 deletions tests/helpers/strings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,46 +293,6 @@ describe('strings', () => {
});
});

describe('startsWith', () => {
it('should return true if the string starts with the provided substring', () => {
expect(strings.startsWith('Hello World!', 'Hello')).toEqual(true);
});

it('should return false if the string does not start with the provided substring', () => {
expect(strings.startsWith('Hello World!', 'World')).toEqual(false);
});

it('should work as expected after compilation (Basic Support)', () => {
const template = compile('{{startsWith string subString}}');
const obj = {
string: 'Hello World!',
subString: 'Hello'
};

expect(template(obj)).toEqual(true);
});
});

describe('endsWith', () => {
it('should return true if the string ends with the provided substring', () => {
expect(strings.endsWith('Hello World!', 'World!')).toEqual(true);
});

it('should return false if the string does not end with the provided substring', () => {
expect(strings.endsWith('Hello World!', 'Hello')).toEqual(false);
});

it('should work as expected after compilation (Basic Support)', () => {
const template = compile('{{endsWith string subString}}');
const obj = {
string: 'Hello World!',
subString: 'World!'
};

expect(template(obj)).toEqual(true);
});
});

describe('unique', () => {
it('should return unique elements from an array', () => {
expect(strings.unique(['apple', 'banana', 'apple', 'mango', 'banana'])).toEqual(['apple', 'banana', 'mango']);
Expand Down

0 comments on commit d7342df

Please sign in to comment.