`Ember.String.fmt` is deprecated

This commit is contained in:
Robin Ward 2016-11-02 15:37:26 -04:00
parent 464f509941
commit 2d126cff8f
3 changed files with 35 additions and 21 deletions

View File

@ -1,3 +1,4 @@
import addonFmt from 'ember-addons/fmt';
/** /**
Returns whether two properties are equal to each other. 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 @params {String} format the i18n format string
@return {Function} computedProperty function @return {Function} computedProperty function
**/ **/
export function i18n() { export function i18n(...args) {
const args = Array.prototype.slice.call(arguments, 0);
const format = args.pop(); const format = args.pop();
const computed = Em.computed(function() { const computed = Ember.computed(function() {
const self = this; return I18n.t(addonFmt(format, ...args.map(a => this.get(a))));
return I18n.t(format.fmt.apply(format, args.map(function (a) {
return self.get(a);
})));
}); });
return computed.property.apply(computed, args); return computed.property.apply(computed, args);
} }
@ -67,14 +64,10 @@ export function i18n() {
@params {String} format the format string @params {String} format the format string
@return {Function} computedProperty function @return {Function} computedProperty function
**/ **/
export function fmt() { export function fmt(...args) {
const args = Array.prototype.slice.call(arguments, 0);
const format = args.pop(); const format = args.pop();
const computed = Em.computed(function() { const computed = Ember.computed(function() {
const self = this; return addonFmt(format, ...args.map(a => this.get(a)));
return format.fmt.apply(format, args.map(function (a) {
return self.get(a);
}));
}); });
return computed.property.apply(computed, args); return computed.property.apply(computed, args);
} }
@ -88,14 +81,10 @@ export function fmt() {
@params {String} format the format string for the URL @params {String} format the format string for the URL
@return {Function} computedProperty function returning a URL @return {Function} computedProperty function returning a URL
**/ **/
export function url() { export function url(...args) {
const args = Array.prototype.slice.call(arguments, 0);
const format = args.pop(); const format = args.pop();
const computed = Em.computed(function() { const computed = Ember.computed(function() {
const self = this; return Discourse.getURL(addonFmt(format, ...args.map(a => this.get(a))));
return Discourse.getURL(format.fmt.apply(format, args.map(function (a) {
return self.get(a);
})));
}); });
return computed.property.apply(computed, args); return computed.property.apply(computed, args);
} }

View File

@ -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);
});
}

View File

@ -2,6 +2,7 @@
//= require ./ember-addons/decorator-alias //= require ./ember-addons/decorator-alias
//= require ./ember-addons/macro-alias //= require ./ember-addons/macro-alias
//= require ./ember-addons/ember-computed-decorators //= require ./ember-addons/ember-computed-decorators
//= require ./ember-addons/fmt
//= require_tree ./discourse-common //= require_tree ./discourse-common
//= require ./discourse //= require ./discourse
//= require ./deprecated //= require ./deprecated