Skip to content

Commit

Permalink
Add dataurl alias of url (elastic#646)
Browse files Browse the repository at this point in the history
* [Image] Change dataurl to url, dataurl is an alias for bwc

* update tests

* dataurl won't be an arg because it's aliased to url

* make url the alias, keep dataurl as the primary

* revert a pulled-out change and comment
  • Loading branch information
tsullivan authored Jun 8, 2018
1 parent 5965fc9 commit d9333a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
23 changes: 15 additions & 8 deletions common/functions/__tests__/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ describe('image', () => {

describe('args', () => {
describe('dataurl', () => {
it('sets the source of the image', () => {
it('sets the source of the image using dataurl', () => {
const result = fn(null, { dataurl: elasticOutline });
expect(result).to.have.property('dataurl', elasticOutline);
});

it.skip('sets the source of the image using url', () => {
// This is skipped because functionWrapper doesn't use the actual
// interpreter and doesn't resolve aliases
const result = fn(null, { url: elasticOutline });
expect(result).to.have.property('dataurl', elasticOutline);
});

it('defaults to the elasticLogo if not provided', () => {
const result = fn(null);
expect(result).to.have.property('dataurl', elasticLogo);
Expand All @@ -27,25 +34,25 @@ describe('image', () => {

describe('mode', () => {
it('sets the mode', () => {
it('returns an image object using a dataUrl', () => {
it('to contain', () => {
const result = fn(null, { mode: 'contain' });
expect(result).to.have.property('mode', 'contain');
});

it('returns an image object using a dataUrl', () => {
it('to cover', () => {
const result = fn(null, { mode: 'cover' });
expect(result).to.have.property('mode', 'cover');
});

it('returns an image object using a dataUrl', () => {
it('to stretch', () => {
const result = fn(null, { mode: 'stretch' });
expect(result).to.have.property('mode', 'stretch');
});
});

it("defaults to 'contain' if not provided", () => {
const result = fn(null);
expect(result).to.have.property('mode', 'contain');
it("defaults to 'contain' if not provided", () => {
const result = fn(null);
expect(result).to.have.property('mode', 'contain');
});
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions common/functions/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const image = () => ({
// This was accepting dataurl, but there was no facility in fn for checking type and handling a dataurl type.
types: ['string', 'null'],
help: 'Base64 encoded image',
aliases: ['_'],
aliases: ['_', 'url'],
default: elasticLogo,
},
mode: {
Expand All @@ -27,16 +27,16 @@ export const image = () => ({
default: 'contain',
},
},
fn: (context, args) => {
if (!includes(['contain', 'cover', 'stretch'], args.mode))
fn: (context, { dataurl, mode }) => {
if (!includes(['contain', 'cover', 'stretch'], mode))
throw '"mode" must be "contain", "cover", or "stretch"';

const mode = args.mode === 'stretch' ? '100% 100%' : args.mode;
const modeStyle = mode === 'stretch' ? '100% 100%' : mode;

return {
type: 'image',
mode,
dataurl: resolveWithMissingImage(args.dataurl, elasticLogo),
mode: modeStyle,
dataurl: resolveWithMissingImage(dataurl, elasticLogo),
};
},
});
5 changes: 5 additions & 0 deletions common/lib/resolve_dataurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { get } from 'lodash';
import { isValid } from '../../common/lib/dataurl';
import { missingImage } from '../../common/lib/missing_asset';

/*
* NOTE: args.dataurl can come as an expression here.
* For example:
* [{"type":"expression","chain":[{"type":"function","function":"asset","arguments":{"_":["..."]}}]}]
*/
export const resolveFromArgs = (args, defaultDataurl = null) => {
const dataurl = get(args, 'dataurl.0', null);
return isValid(dataurl) ? dataurl : defaultDataurl;
Expand Down

0 comments on commit d9333a8

Please sign in to comment.