From 1fc4adfb193599e4b7ed03ce313027c14afd36f9 Mon Sep 17 00:00:00 2001 From: Steven Maguire Date: Sun, 9 Oct 2016 08:07:37 -0500 Subject: [PATCH] Update organization method to return only non-empty unique strings --- CHANGELOG.md | 17 +++++++++++++++++ app.js | 37 ++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e0253b..df4117e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ All Notable changes to `chargify-zendesk` will be documented in this file +## 1.3.1 - 2016-10-09 + +### Added +- User organization domains will only include unique values, reducing duplicative API calls to Chargify + +### Deprecated +- Nothing + +### Fixed +- Users with no organization domains returning results for `/customers.json?q=@` - thanks @jmatthewpryor + +### Removed +- Nothing + +### Security +- Nothing + ## 1.3.0 - 2016-10-06 ### Added diff --git a/app.js b/app.js index a4dcd6d..684d741 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,19 @@ (function() { + if (typeof Array.prototype.getUnique == 'undefined') { + Array.prototype.getUnique = function () { + var u = {}, a = []; + for (var i = 0, l = this.length; i < l; ++i) { + if(u.hasOwnProperty(this[i])) { + continue; + } + a.push(this[i]); + u[this[i]] = 1; + } + return a; + }; + } + return { resources: { DOMAIN_PATTERN: /[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/, @@ -139,7 +153,14 @@ }, getCustomerOrganizationDomains: function () { - var organization, user; + var organization, user, domains = []; + + var appendDomain = function (domain) { + domain = (domain + '').trim(); + if (domain.length) { + domains.push(domain); + } + }; if (this.currentLocation() === 'ticket_sidebar') { user = this.ticket().requester(); @@ -150,23 +171,17 @@ } if (organization) { - return organization.domains().split(' '); + organization.domains().split(' ').map(appendDomain); } if (user) { - var organizations = user.organizations(), - domains = []; - + var organizations = user.organizations(); organizations.map(function (organization) { - organization.domains().split(' ').map(function (domain) { - domains.push(domain); - }); + organization.domains().split(' ').map(appendDomain); }); - - return domains; } - return; + return domains.length ? domains.getUnique() : null; }, getDomainFromURL: function(baseURI) {