social-links
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -15,2 +15,3 @@ export interface ProfileMatch { | ||
usePredefinedProfiles?: boolean; | ||
trimInput?: boolean; | ||
} | ||
@@ -22,2 +23,3 @@ export declare const DEFAULT_CONFIG: Config; | ||
constructor(config?: Config | boolean); | ||
private trim; | ||
addProfile(profileName: string, profileMatches: ProfileMatch[]): boolean; | ||
@@ -24,0 +26,0 @@ cleanProfiles(): void; |
@@ -32,2 +32,3 @@ "use strict"; | ||
usePredefinedProfiles: true, | ||
trimInput: true, | ||
}; | ||
@@ -50,2 +51,5 @@ var SocialLinks = /** @class */ (function () { | ||
} | ||
SocialLinks.prototype.trim = function (input) { | ||
return this.config.trimInput ? input.trim() : input; | ||
}; | ||
SocialLinks.prototype.addProfile = function (profileName, profileMatches) { | ||
@@ -64,3 +68,3 @@ if (this.hasProfile(profileName)) | ||
var matches = this.profiles.get(profileName); | ||
return findIndex(matches, link) !== -1; | ||
return findIndex(matches, this.trim(link)) !== -1; | ||
}; | ||
@@ -72,6 +76,7 @@ SocialLinks.prototype.getProfileId = function (profileName, link) { | ||
var matches = (_a = this.profiles.get(profileName)) !== null && _a !== void 0 ? _a : []; | ||
var idx = findIndex(matches, link); | ||
var trimmed = this.trim(link); | ||
var idx = findIndex(matches, trimmed); | ||
if (idx === -1) | ||
throw new Error("Link has not matched with profile " + profileName); | ||
return ((_b = link.match(createRegexp(matches[idx].match))) !== null && _b !== void 0 ? _b : [])[matches[idx].group]; | ||
return ((_b = trimmed.match(createRegexp(matches[idx].match))) !== null && _b !== void 0 ? _b : [])[matches[idx].group]; | ||
}; | ||
@@ -92,3 +97,3 @@ SocialLinks.prototype.getLink = function (profileName, id, type) { | ||
throw new Error("There is no pattern for profile " + profileName); | ||
return ((_b = matches[idx].pattern) !== null && _b !== void 0 ? _b : '').replace('{PROFILE_ID}', "" + id); | ||
return ((_b = matches[idx].pattern) !== null && _b !== void 0 ? _b : '').replace('{PROFILE_ID}', "" + this.trim(id)); | ||
}; | ||
@@ -98,5 +103,6 @@ SocialLinks.prototype.sanitize = function (profileName, link, type) { | ||
if (type === void 0) { type = TYPE_DEFAULT; } | ||
var profileId = this.getProfileId(profileName, link); | ||
var trimmed = this.trim(link); | ||
var profileId = this.getProfileId(profileName, trimmed); | ||
var matches = (_a = this.profiles.get(profileName)) !== null && _a !== void 0 ? _a : []; | ||
var idx = findIndex(matches, link); | ||
var idx = findIndex(matches, trimmed); | ||
var matchedType = type !== TYPE_DEFAULT ? type : ((_b = matches[idx].type) !== null && _b !== void 0 ? _b : TYPE_DEFAULT); | ||
@@ -103,0 +109,0 @@ return this.getLink(profileName, profileId, matchedType); |
@@ -217,9 +217,28 @@ "use strict"; | ||
describe('config', function () { | ||
it('should set usePredefinedProfiles = true', function () { | ||
sl = new main_1.SocialLinks({ usePredefinedProfiles: true }); | ||
expect(sl.getLink('linkedin', 'gkucmierz')).toBe('https://linkedin.com/in/gkucmierz'); | ||
describe('usePredefinedProfiles', function () { | ||
it('should set usePredefinedProfiles = true', function () { | ||
sl = new main_1.SocialLinks({ usePredefinedProfiles: true }); | ||
expect(sl.getLink('linkedin', 'gkucmierz')).toBe('https://linkedin.com/in/gkucmierz'); | ||
}); | ||
it('should set usePredefinedProfiles = false', function () { | ||
sl = new main_1.SocialLinks({ usePredefinedProfiles: false }); | ||
expect(function () { return sl.getLink('linkedin', 'gkucmierz'); }).toThrowError(); | ||
}); | ||
it('should set trimInput as default', function () { | ||
sl = new main_1.SocialLinks(); | ||
var whitespace = [' ', '\t', '\n'].join(''); | ||
expect(sl.isValid('linkedin', whitespace + "http://www.linkedin.com/in/gkucmierz" + whitespace)).toBeTruthy(); | ||
}); | ||
}); | ||
it('should set usePredefinedProfiles = false', function () { | ||
sl = new main_1.SocialLinks({ usePredefinedProfiles: false }); | ||
expect(function () { return sl.getLink('linkedin', 'gkucmierz'); }).toThrowError(); | ||
describe('trimInput', function () { | ||
it('should trim isValid', function () { | ||
sl = new main_1.SocialLinks({ trimInput: true }); | ||
var whitespace = [' ', '\t', '\n'].join(''); | ||
expect(sl.isValid('linkedin', whitespace + "http://www.linkedin.com/in/gkucmierz" + whitespace)).toBeTruthy(); | ||
}); | ||
it('should not trim isValid', function () { | ||
sl = new main_1.SocialLinks({ trimInput: false }); | ||
var whitespace = [' ', '\t', '\n'].join(''); | ||
expect(sl.isValid('linkedin', whitespace + "http://www.linkedin.com/in/gkucmierz" + whitespace)).toBeFalsy(); | ||
}); | ||
}); | ||
@@ -226,0 +245,0 @@ }); |
{ | ||
"name": "social-links", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Validate & sanitize social links", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
46737
533