Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-custom-directives",
"version": "0.2.13",
"version": "0.2.14",
"description": "Create custom graphql directives with the ability to hook the execution of graphql",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/directives/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ exports.GraphQLCurrencyDirective = new GraphQLCustomDirective({
type: GraphQLString,
description: 'A currency format given by numeral module',
},
currencySymbol: {
type: GraphQLString,
description: 'A currency symbol to attach to the output (Numeral supports only one currency at a time, so this is an override)'
}
},
resolve(resolve, source, {as}) {
resolve(resolve, source, {as, currencySymbol = ''}) {
return resolve().then(input => {
const format = as || DEFAULT_CURRENCY_FORMAT;

if (format.indexOf('0') !== -1 && !Number.isNaN(Number(input))) {
return numeral(input).format(format) || input;
return `${currencySymbol}${numeral(input).format(format) || input}`;
}

return input;
Expand Down
8 changes: 8 additions & 0 deletions test/directives/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ describe('directives/currency', () => {

testEqual({directives, query, expected, done});
});

it('expected directive to use the supplied currency symbol', done => {
const query = `{ value(input: "10") @currency(as:"0,0.00", currencySymbol: "£") }`,
directives = [GraphQLCurrencyDirective],
expected = {value: '£10.00'};

testEqual({directives, query, expected, done});
});
});