Comments (15)
Thanks, these are good suggestions.
formatNumber: This is a great candidate for a future release. I would suggest aligning it with the two-argument form of the XPath format-number function as described here: https://www.w3.org/TR/xpath-functions-31/#func-format-number since that is quite flexible.
formatDate: This is potentially a bottomless pit because of issues around localization, etc. I added a $now() function in v1.2 which generates an ISO8601 formatted timestamp. Beyond that, I would be inclined to defer to the excellent moment library which can manipulate and format date/times in any way imaginable. It is very easy to plug in moment as an extension function in JSONata using the following...
var moment = require('moment');
var jsonata = require('jsonata');
var expr = jsonata('$moment().format("DD/MM/YYYY HH:mm:ss")');
expr.assign('moment', function (arg1, arg2, arg3, arg4) {
return moment(arg1, arg2, arg3, arg4);
});
var result = expr.evaluate();
Equivalent code can be written for use in the browser
from jsonata.
$formatNumber() has now been added to the 1.4
branch. Implemented in accordance with the XPath fn:format-number spec. The function takes 3 arguments (third one optional):
- The number to be formatted.
- The picture string (see spec for details)
- Optional locale overrides. An object whose key/value pairs specify alternative characters for numeric/decimal/grouping/exponent/etc characters. Aligned with XPath spec.
from jsonata.
@andrew-coleman - i agree with you on the formatNumber. It can be matched to the xpath format-number capability. Hope to get this in the next release.
For format-Date, would it not be better to include this library inside jsonata itself? That way, this piece of code can be abstracted from each calling application? It is just a suggestion as I completely understand your reasoning on why you dont want to as well.
from jsonata.
@andrew-coleman do you have any estimate on the date of the next release?
from jsonata.
@zubairov - v1.3 was released on Friday. I don't have specific dates for future releases, but I hope to have more frequent smaller releases as and when new functions/operators/syntax are developed. Presumably you are interested in these format functions?
from jsonata.
Yes. Would be great to know when do you think the formatNumber
will become a part of build-in function set. The date parsing and transformation functions we'll add in 'userspace'
from jsonata.
@zubairov I understand the need for this, and I think the XPath fn:format-number function has the most flexible and usable syntax for this purpose.
If you are willing to contribute, I would happily accept a pull request that contained an implementation of this. Let me know if that's a possibility :)
Thanks!
from jsonata.
@andrew-coleman I'm trying to integrate moment.js
. Your example works fine, but only with "instance" methods of Moment.js. How to make statics working?
I've tried to do this:
expr.assign('moment', moment);
but it works in the same way, like your example. With $moment.isMoment()
expression it throws Attempted to invoke a non-function
.
from jsonata.
@pnedelko not sure about this one. moment
identifies itself as a function (using typeof moment
), so jsonata would expect it to be invoked as a function rather than to have properties like a JSON object (which is why your $moment.isMoment
returns nothing and hence can't be invoked).
Also jsonata assumes that all values are immutable and that functions don't have side effects, so it looks like this aspect of moment doesn't fit that functional model.
from jsonata.
@andrew-coleman - is there a plan to include the format-number function in jsonata? Can we expect it in a near future release?
from jsonata.
Hi @raj-work, yes I do plan to add this function to a future release, although I haven't started implementing it yet. I will get round to doing it, but I have to balance this with demands from my day job. If anyone subscribed to this issue would care to help, then I'd happily accept a pull request.
Thanks :)
from jsonata.
great, thanks! Whats the ETA for 1.4?
from jsonata.
One of the areas where JSONata is harder to use than other languages/tools is in string handling. Here are a few thoughts I've had about new functionality that would have made some of my expressions easier.
Occasionally it would be handy to be able to convert letters to their number equivalents, such as $ascii(char)
. This could be an extension to the existing $number(arg)
function, but I suspect that as soon as you are able to convert 'a' to 65, someone will want the EBCIDIC value as well... sigh. So probably better to spell out the function by name.
If the arg is a string of length > 1, then I would expect probably an array of numbers. Also, I would love to be able to use the ..
operator to interpolate between letters, so I could easily generate an alphabet array of letters using [ 'A' .. 'Z' ]
-- good for splitting things into groups, such as index entries for book titles.
And as long as we are treating letters as ASCII values, I can see where aggregation functions like $min(array)
and $max(array)
would be handy. The sort functions are already able to compare strings "numerically", so it's almost as easy to sort asc/desc and just take the first element to get the min/max "value". However, I think that having overloaded aggregate functions would be more readable.
from jsonata.
formatNumber()
now published in v1.4
from jsonata.
Awesome, thanks allot!
from jsonata.
Related Issues (20)
- ($.some_non_existent_key != 'anything') evaluates to false HOT 3
- $string(number) capped at precision-15
- Error handling and asynchronous behavior in evaluate function
- Rounding number without "Round half to even"
- There isn't a uuid() function for JSONata HOT 2
- Hard spaces in documentation not rendered correctly
- Security contact for JSONata HOT 4
- default query on try.jsonata.org HOT 1
- is 2.0.4 a fake release? HOT 4
- Multi-level JSON processing: Unwrapping Objects
- How to work with parent object and transform values
- Stronger `registerFunction` TS types by inferring types from signature, and a road to a language service (PR coming!)
- Feature: Jsonata json type inference HOT 5
- JSONata leaks internal references in expression when using lambdas
- JSONAta expression can be created that ends up in mutation of original input object
- `__evaluate_entry` and `__evaluate_exit` can be overridden inside expression
- $sort fails for >6000-element arrays HOT 1
- Why is jsonata async/await? HOT 2
- Unexpected array access behaviour HOT 1
- Missing Type in Interface ExprNode, lhs can be both an ExprNode and ExprNode[]
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonata.