-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from chukwumaijem/feature/get-gravatar-url
Basic Gravatar URL
- Loading branch information
Showing
8 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import md5 from 'md5'; | ||
import { Types, Errors } from './types'; | ||
|
||
export function validateOptions(options) { | ||
const { email, md5Hash, size, defaultImage } = options; | ||
if (!email && !md5Hash) throw new Error(Errors.MISSING_ARGUMENT_ERROR('email or md5Hash')); | ||
|
||
if (size && Number.isNaN(Number(size))) throw new Error(Errors.INVALID_ARGUMENT_TYPE(size, 'number')); | ||
|
||
if (size > 2048 || size < 1) throw new Error(Errors.IMAGE_SIZE_ERROR); | ||
|
||
if (defaultImage && !Types.GRAVATAR_DEFAULTS.includes(defaultImage)) throw new Error(Errors.DEFAULT_IMAGE_ERROR); | ||
} | ||
|
||
function getMd5Hash(options) { | ||
const { email, md5Hash } = options; | ||
if (md5Hash) return md5Hash; | ||
|
||
return md5(email.trim().toLowerCase()); | ||
} | ||
|
||
export function buildQueryStringFromOptions(options) { | ||
const { size, defaultImage } = options; | ||
let queryString = '?'; | ||
if (size) queryString += `s=${size}&`; | ||
if (defaultImage) queryString += `d=${defaultImage}&`; | ||
|
||
if (queryString.endsWith('&') || queryString.endsWith('?')) { | ||
return queryString.substr(0, queryString.length - 1); | ||
} | ||
return queryString; | ||
} | ||
|
||
function jsGravatar(options) { | ||
validateOptions(options); | ||
const emailHash = getMd5Hash(options); | ||
const queryString = buildQueryStringFromOptions(options); | ||
|
||
return `${Types.BASE_URL}/${emailHash}${queryString}`; | ||
} | ||
|
||
export default jsGravatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const Types = { | ||
BASE_URL: (() => { | ||
const protocol = window.location.protocol; | ||
const gravatar_url = '//www.gravatar.com/avatar'; | ||
if (protocol !== 'https:') { | ||
return `http:${gravatar_url}`; | ||
} | ||
return `https:${gravatar_url}`; | ||
})(), | ||
GRAVATAR_DEFAULTS: ['404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank'], | ||
}; | ||
|
||
export const Errors = { | ||
MISSING_ARGUMENT_ERROR: (arg) => `${arg} is required but was not provided.`, | ||
INVALID_ARGUMENT_TYPE: (arg, type) => `${arg} is expected to be of type ${type}, but received ${typeof arg}`, | ||
DEFAULT_IMAGE_ERROR: `defaultImage can only be oneOf: ${Types.GRAVATAR_DEFAULTS.join(',')}`, | ||
IMAGE_SIZE_ERROR: 'Size should not be greater than 2048 or less than 1.', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>JS Gravatar - Demo</title> | ||
</head> | ||
|
||
<body> | ||
<div> | ||
<img id="image" /> | ||
</div> | ||
|
||
<script src="../dist/js-gravatar.js"></script> | ||
<script> | ||
const imageSrc = jsGravatar({ email: 'user@email.com', defaultImage: 'identicon' }); | ||
const image = document.getElementById('image'); | ||
image.src = imageSrc; | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
import jsGravatar, { buildQueryStringFromOptions, validateOptions } from '../lib/js-gravatar'; | ||
import { Types, Errors } from '../lib/types'; | ||
|
||
describe('JS Gravatar', function () { | ||
it('1 + 1 should be 2', () => { | ||
expect(1 + 1).toEqual(2); | ||
describe('Throws an error', function () { | ||
it('for missing email and md5hash', function () { | ||
expect(() => validateOptions({ defaultImage: '404' })).toThrow(Errors.MISSING_ARGUMENT_ERROR('email or md5Hash')); | ||
}); | ||
|
||
it('for invalid size options.', function () { | ||
expect(() => validateOptions({ email: 'user@email.com', size: 0 })).toThrow(Errors.IMAGE_SIZE_ERROR); | ||
expect(() => validateOptions({ email: 'user@email.com', size: 2049 })).toThrow(Errors.IMAGE_SIZE_ERROR); | ||
}); | ||
|
||
it('for unexpected default image.', function () { | ||
expect(() => validateOptions({ email: 'user@email.com', defaultImage: 'not-available' })).toThrow( | ||
Errors.DEFAULT_IMAGE_ERROR, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Correctly generates url query string', function() { | ||
const size = 200; | ||
const defaultImage = '404'; | ||
|
||
it('when only one property is supplied.', function () { | ||
expect(buildQueryStringFromOptions({ size })).toBe(`?s=${size}`); | ||
expect(buildQueryStringFromOptions({ defaultImage })).toBe(`?d=${defaultImage}`); | ||
}); | ||
|
||
it('when both size and defaultImage are supplied.', function () { | ||
expect(buildQueryStringFromOptions({ defaultImage, size })).toBe(`?s=${size}&d=${defaultImage}`); | ||
}); | ||
|
||
it('when no property is available.', function () { | ||
expect(buildQueryStringFromOptions({})).toBe(''); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters