-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format numbers with delimiters #77
Comments
The
|
So if every user is expecting to receive count etc as formatted numbers. Proposal:
Doing all this work and adding attributes in a deep way (remembering the parent) should be fun. We can try this in the instantsearch.js and if it fits move it to the helper where it should belongs I guess (everything auto formatting data seems to fit in the helper). |
Adding Proposal:
|
Yes, so if done like this it should be deactivated by default
Excactly what I wanted to add here. 💯 for this proposal!
When providing a function we do not render as hogan. Template as function is the way to provide our users "do you have another templating engine? use it!". We can still expose So that he can reuse it. @pixelastic Can you udpate the issue + API proposal and do the PR? Looks cool |
Do you think to auto formatting of numeric values through the |
No we only need to do exactly what you proposed (BUT the "function return string is hogan" because function as template should not be treated as hogan). |
To clarify:
|
I updated the first comment (by @redox) already. I'll take this PR soon. |
With your proposal @pixelastic (see top description) It looks like we may need to do the full named parameters instantiation right away because with: var search = instantsearch('latency', '6be0576ff61c053d5f9a3225e2a90f76', 'instant_search', {
numberLocale: 'fr_FR'
}); You need to remember the order of 4 arguments. var search = instantsearch(
appId: 'latency',
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
indexName: 'instant_search',
numberLocale: 'fr_FR'
defaultSearchParameters: {
hitsPerPage: 100
}
); This looks to me more precise and less error prone. What do you think @algolia/javascript? That's another issue but need to tackle it before we go public, if you agree Ill open the corresponding issue. |
Oh yes, I forgot about the |
Where did you actually see that if was possible to define I'll keep trying. |
Strange mustache.js readme does state this example works https://github.com/janl/mustache.js/blob/master/README.md#functions |
Yep, that's what I tried. But I don't have the second argument ( |
Maybe its a hogan issue, will see |
@jerskouille can you help, I know you're used to do that. |
Oh yeah twitter/hogan.js#222 with comments from @jerskouille :) |
Yes, lambdas in Hogan have a big mismatch with the docs. By the way, to answer this:
I believe it kind of is the same thing with the method, since I'm not aware of any technique to inject the method without adding it at the root of the hit object. {
hit: hit,
helpers: {
formatNumber: function (text, render) { ... }
}
} |
Thanks for the debug! So in regard with all this new info, this is what I suggest:
I used the leading I'll now need to pass this helper method all the way from the instantsearch instance to the render method. |
You're right, people already have access to the instance here, no need to add the method. |
But, this brings another point. I'll have to edit the current I could pass the The last one one feels a bit overkill, though. In any case, this means editing the // Current method
_render(helper, results, state) {
forEach(this.widgets, function(widget) {
if (widget.render) {
widget.render(results, state, helper);
}
});
}
// Suggested change
_render(helper, results, state) {
var instansearch = this;
// or var numberLocale = this.numberLocale (en-EN)
// or var formatNumber = this.formatNumber (method correctly bound to en-EN)
// or var templateHelpers = this.templateHelpers (list of template methods, correctly bound)
forEach(this.widgets, function(widget) {
if (widget.render) {
widget.render({
instantsearch: instantsearch,
// or numberLocale: numberLocal
// or formatNumber: formatNumber
// or templateHelpers: templateHelpers,
results: results,
state: state,
helper: helper
});
}
});
} Note that if we pass the whole |
Maybe just // Suggested change
_render(helper, results, state) {
forEach(this.widgets, function(widget) {
if (widget.render) {
widget.render({
utils: this.utils,
results,
helper,
state
});
}
}, this);
} |
this was done and merged |
Users in different languages will need to format the display of numbers (eg.
100000
displayed as100,000
).We should give them an easy way to do it in their templates.
API proposal:
en_EN
as the default language for the number formatting. Let users override it when instanciatinginstantsearch
through thenumberLocale
option.formatNumber
method that will be available both throughmy_instantsearch_instance.utils.formatNumber
and inside Hogan templates through the{{#formatNumber}}{{my_var}}{{/formatNumber}}
syntax that will format the number according to the current locale.Examples
The text was updated successfully, but these errors were encountered: