Skip to content

Commit e397e73

Browse files
committed
added trim function
1 parent 9093fbf commit e397e73

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

dist/breinify-api.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12551,7 +12551,7 @@ dependencyScope.jQuery = $;;
1255112551

1255212552
/**
1255312553
* Checks if the passed value is empty, i.e., is an empty string (trimmed), an empty object, undefined or null.
12554-
* @param val {mixed} the value to be checked
12554+
* @param val {*} the value to be checked
1255512555
* @returns {boolean} true if the value is empty, otherwise false
1255612556
*/
1255712557
isEmpty: function (val) {
@@ -12571,6 +12571,22 @@ dependencyScope.jQuery = $;;
1257112571
}
1257212572
},
1257312573

12574+
/**
12575+
* Removes quotes from a string, e.g., "Test" => Test
12576+
* @param str the string to remove the quotes from
12577+
* @param {boolean} inclSingle can be true if also single quotes like ' should be removed, be careful something like "Test' would end up to be Test
12578+
* @returns {*} if the passed value is not a string, this value will be returned, otherwise the trimmed str, without any surrounding quotes will be returned
12579+
*/
12580+
trimQuotes: function (str, inclSingle) {
12581+
if (str == null || typeof str != 'string') {
12582+
return str;
12583+
}
12584+
12585+
var quotes = inclSingle === true ? '["\']' : '["]';
12586+
var regEx = '^' + quotes + '(.*)' + quotes + '$';
12587+
return str.replace(new RegExp(regEx), '$1');
12588+
},
12589+
1257412590
isSimpleObject: function (obj) {
1257512591
if (obj == null) {
1257612592
return true;
@@ -13258,7 +13274,7 @@ dependencyScope.jQuery = $;;
1325813274
},
1325913275

1326013276
generateRecommendationMessage: function (amount, unixTimestamp) {
13261-
return unixTimestamp + amount;
13277+
return '' + unixTimestamp + amount;
1326213278
},
1326313279

1326413280
generateLookUpMessage: function (dimensions, unixTimestamp) {
@@ -13378,7 +13394,7 @@ dependencyScope.jQuery = $;;
1337813394
if (sign) {
1337913395
var secret = _config.get(ATTR_CONFIG.SECRET);
1338013396
if (typeof secret === 'string') {
13381-
var message = _privates.generateRecommendationMessage(1, unixTimestamp, type);
13397+
var message = _privates.generateRecommendationMessage(nrOfRecommendations, unixTimestamp);
1338213398
signature = _privates.determineSignature(message, _config.get(ATTR_CONFIG.SECRET))
1338313399
} else {
1338413400
_onReady(null);
@@ -13748,6 +13764,16 @@ dependencyScope.jQuery = $;;
1374813764
onReady();
1374913765
}
1375013766
};
13767+
Breinify.recommendation = function (uuser, nr, sign, onReady) {
13768+
if (typeof onReady === 'function') {
13769+
onReady();
13770+
}
13771+
};
13772+
Breinify.recommendationUser = function (user, nr, sign, onReady) {
13773+
if (typeof onReady === 'function') {
13774+
onReady();
13775+
}
13776+
};
1375113777
Breinify.UTL = {
1375213778
loc: {
1375313779
params: function () { return []; },
@@ -13776,6 +13802,7 @@ dependencyScope.jQuery = $;;
1377613802
unixTimestamp: function () {
1377713803
return Math.floor(new Date().getTime() / 1000);
1377813804
},
13805+
trimQuotes: function(str) { return str; },
1377913806
setText: function() {},
1378013807
md5: function () { return null; },
1378113808
isEmpty: function() { return false; },

dist/breinify-api.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

documentation/api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This documentation is organized as following:
2222
* Breinify.lookup(user, dimensions, sign, onLookUp)
2323
* [Utilities (UTL)](#utilities-utl)
2424
* [Breinify.UTL (general functions)](#breinifyutl-general-functions)
25+
* Breinify.UTL.trimQuotes(str, inclSingleQuotes)
2526
* Breinify.UTL.texts(selector, excludeChildren)
2627
* Breinify.UTL.text(selector, excludeChildren)
2728
* Breinify.UTL.setText(selector, value)
@@ -203,6 +204,22 @@ The utility library provides general functionality, which makes it easy to retri
203204

204205
##### Breinify.UTL (general functions)
205206

207+
* {[string]} **Breinify.UTL.trimQuotes(str, inclSingleQuotes)**:<br/>
208+
Trims a string by removing quotes, i.e. if *inclSingleQuotes* is *true* *"* and *'*, otherwise only *"*. It should be noted, that a value like "test' leads to test, if *inclSingleQuotes* is set to *true*.
209+
210+
**Parameters**:
211+
212+
{string} ****: The string to be trimmed
213+
214+
{boolean|null}: **inclSingleQuotes**: true, if only *"* should be removed, otherwise *'*
215+
216+
**Example Usage**:
217+
```javascript
218+
var trimmedText = Breinify.UTL.trimQuotes('"Hello World"', false);
219+
console.log('"Hello World"', trimmedText);
220+
```
221+
<br/>
222+
206223
* {[string]} **Breinify.UTL.texts(selector, excludeChildren)**:<br/>
207224
Gets the text of the elements selected by the specified *selector*.
208225

specs/BreinifyUtil-spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,18 @@ describe('BreinifyUtil', function () {
119119
//noinspection JSUnresolvedFunction
120120
expect(Breinify.UTL.isSimpleObject({'val1': 1, 'val2': 2, 'val3': []})).toBe(true);
121121
});
122+
123+
//noinspection JSUnresolvedFunction
124+
it('trimQuotes', function() {
125+
//noinspection JSUnresolvedFunction
126+
expect(Breinify.UTL.trimQuotes(null)).toBeNull();
127+
//noinspection JSUnresolvedFunction
128+
expect(Breinify.UTL.trimQuotes({})).toEqual({});
129+
//noinspection JSUnresolvedFunction
130+
expect(Breinify.UTL.trimQuotes('"Test"')).toEqual('Test');
131+
//noinspection JSUnresolvedFunction
132+
expect(Breinify.UTL.trimQuotes(' "Test" ')).toEqual(' "Test" ');
133+
//noinspection JSUnresolvedFunction
134+
expect(Breinify.UTL.trimQuotes('\'Test\'', true)).toEqual('Test');
135+
});
122136
});

src/BreinifyUtil.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353

354354
/**
355355
* Checks if the passed value is empty, i.e., is an empty string (trimmed), an empty object, undefined or null.
356-
* @param val {mixed} the value to be checked
356+
* @param val {*} the value to be checked
357357
* @returns {boolean} true if the value is empty, otherwise false
358358
*/
359359
isEmpty: function (val) {
@@ -373,6 +373,22 @@
373373
}
374374
},
375375

376+
/**
377+
* Removes quotes from a string, e.g., "Test" => Test
378+
* @param str the string to remove the quotes from
379+
* @param {boolean} inclSingle can be true if also single quotes like ' should be removed, be careful something like "Test' would end up to be Test
380+
* @returns {*} if the passed value is not a string, this value will be returned, otherwise the trimmed str, without any surrounding quotes will be returned
381+
*/
382+
trimQuotes: function (str, inclSingle) {
383+
if (str == null || typeof str != 'string') {
384+
return str;
385+
}
386+
387+
var quotes = inclSingle === true ? '["\']' : '["]';
388+
var regEx = '^' + quotes + '(.*)' + quotes + '$';
389+
return str.replace(new RegExp(regEx), '$1');
390+
},
391+
376392
isSimpleObject: function (obj) {
377393
if (obj == null) {
378394
return true;

src/snippets/suffix-global.js.snippet

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353
onReady();
5454
}
5555
};
56+
Breinify.recommendation = function (uuser, nr, sign, onReady) {
57+
if (typeof onReady === 'function') {
58+
onReady();
59+
}
60+
};
61+
Breinify.recommendationUser = function (user, nr, sign, onReady) {
62+
if (typeof onReady === 'function') {
63+
onReady();
64+
}
65+
};
5666
Breinify.UTL = {
5767
loc: {
5868
params: function () { return []; },
@@ -81,6 +91,7 @@
8191
unixTimestamp: function () {
8292
return Math.floor(new Date().getTime() / 1000);
8393
},
94+
trimQuotes: function(str) { return str; },
8495
setText: function() {},
8596
md5: function () { return null; },
8697
isEmpty: function() { return false; },

0 commit comments

Comments
 (0)