From 2d126cff8f138c55fcea6f6d24d13bf339592b03 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 2 Nov 2016 15:37:26 -0400 Subject: [PATCH] `Ember.String.fmt` is deprecated --- .../javascripts/discourse/lib/computed.js.es6 | 31 ++++++------------- .../javascripts/ember-addons/fmt.js.es6 | 24 ++++++++++++++ app/assets/javascripts/main_include.js | 1 + 3 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 app/assets/javascripts/ember-addons/fmt.js.es6 diff --git a/app/assets/javascripts/discourse/lib/computed.js.es6 b/app/assets/javascripts/discourse/lib/computed.js.es6 index 1fb7e745cb5..02d38869fe2 100644 --- a/app/assets/javascripts/discourse/lib/computed.js.es6 +++ b/app/assets/javascripts/discourse/lib/computed.js.es6 @@ -1,3 +1,4 @@ +import addonFmt from 'ember-addons/fmt'; /** Returns whether two properties are equal to each other. @@ -46,14 +47,10 @@ export function propertyLessThan(p1, p2) { @params {String} format the i18n format string @return {Function} computedProperty function **/ -export function i18n() { - const args = Array.prototype.slice.call(arguments, 0); +export function i18n(...args) { const format = args.pop(); - const computed = Em.computed(function() { - const self = this; - return I18n.t(format.fmt.apply(format, args.map(function (a) { - return self.get(a); - }))); + const computed = Ember.computed(function() { + return I18n.t(addonFmt(format, ...args.map(a => this.get(a)))); }); return computed.property.apply(computed, args); } @@ -67,14 +64,10 @@ export function i18n() { @params {String} format the format string @return {Function} computedProperty function **/ -export function fmt() { - const args = Array.prototype.slice.call(arguments, 0); +export function fmt(...args) { const format = args.pop(); - const computed = Em.computed(function() { - const self = this; - return format.fmt.apply(format, args.map(function (a) { - return self.get(a); - })); + const computed = Ember.computed(function() { + return addonFmt(format, ...args.map(a => this.get(a))); }); return computed.property.apply(computed, args); } @@ -88,14 +81,10 @@ export function fmt() { @params {String} format the format string for the URL @return {Function} computedProperty function returning a URL **/ -export function url() { - const args = Array.prototype.slice.call(arguments, 0); +export function url(...args) { const format = args.pop(); - const computed = Em.computed(function() { - const self = this; - return Discourse.getURL(format.fmt.apply(format, args.map(function (a) { - return self.get(a); - }))); + const computed = Ember.computed(function() { + return Discourse.getURL(addonFmt(format, ...args.map(a => this.get(a)))); }); return computed.property.apply(computed, args); } diff --git a/app/assets/javascripts/ember-addons/fmt.js.es6 b/app/assets/javascripts/ember-addons/fmt.js.es6 new file mode 100644 index 00000000000..191fbe20ac2 --- /dev/null +++ b/app/assets/javascripts/ember-addons/fmt.js.es6 @@ -0,0 +1,24 @@ +import Ember from 'ember'; + +const inspect = Ember.inspect; +const isArray = Ember.isArray; + +export default function(str, formats) { + let cachedFormats = formats; + + if (!isArray(cachedFormats) || arguments.length > 2) { + cachedFormats = new Array(arguments.length - 1); + + for (let i = 1, l = arguments.length; i < l; i++) { + cachedFormats[i - 1] = arguments[i]; + } + } + + // first, replace any ORDERED replacements. + let idx = 0; // the current index for non-numerical replacements + return str.replace(/%@([0-9]+)?/g, function(s, argIndex) { + argIndex = (argIndex) ? parseInt(argIndex, 10) - 1 : idx++; + s = cachedFormats[argIndex]; + return (s === null) ? '(null)' : (s === undefined) ? '' : inspect(s); + }); +} diff --git a/app/assets/javascripts/main_include.js b/app/assets/javascripts/main_include.js index 440edeee65f..96b3abcd9d3 100644 --- a/app/assets/javascripts/main_include.js +++ b/app/assets/javascripts/main_include.js @@ -2,6 +2,7 @@ //= require ./ember-addons/decorator-alias //= require ./ember-addons/macro-alias //= require ./ember-addons/ember-computed-decorators +//= require ./ember-addons/fmt //= require_tree ./discourse-common //= require ./discourse //= require ./deprecated