From ae79c1c5aea940c865bda791dff70c21f0718ab8 Mon Sep 17 00:00:00 2001 From: Chris Neale Date: Thu, 7 Mar 2019 14:35:53 +0000 Subject: [PATCH] Add new filter to format long numbers according to GOV.UK content guidelines, inserting commas every third digit eg. 1000000 becomes 1,000,000 --- app/filters.js | 5 +++++ package.json | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/filters.js b/app/filters.js index 47ab9ed0ec..06e7c9569e 100644 --- a/app/filters.js +++ b/app/filters.js @@ -1,3 +1,4 @@ +var dateFilter = require('nunjucks-date-filter') module.exports = function (env) { /** * Instantiate object used to store the methods registered as a @@ -6,6 +7,9 @@ module.exports = function (env) { * @type {Object} */ var filters = {} + filters.formatNumber = function (x) { + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + } /* ------------------------------------------------------------------ add your methods to the filters obj below this comment block: @@ -41,5 +45,6 @@ module.exports = function (env) { /* ------------------------------------------------------------------ keep the following line to return your filters to the app ------------------------------------------------------------------ */ + filters.date = dateFilter return filters } diff --git a/package.json b/package.json index 65a8f15841..6ce855d188 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "require-dir": "^1.0.0", "sync-request": "^6.0.0", "universal-analytics": "^0.4.16", - "uuid": "^3.2.1" + "uuid": "^3.2.1", + "nunjucks-date-filter": "^0.1.1" }, "greenkeeper": { "ignore": [