Giter Club home page Giter Club logo

Comments (15)

andrew-coleman avatar andrew-coleman commented on August 22, 2024 1

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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024 1

$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):

  1. The number to be formatted.
  2. The picture string (see spec for details)
  3. 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.

raj-work avatar raj-work commented on August 22, 2024

@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.

zubairov avatar zubairov commented on August 22, 2024

@andrew-coleman do you have any estimate on the date of the next release?

from jsonata.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

@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.

zubairov avatar zubairov commented on August 22, 2024

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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

@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.

pnedelko avatar pnedelko commented on August 22, 2024

@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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

@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.

raj-work avatar raj-work commented on August 22, 2024

@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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

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.

zubairov avatar zubairov commented on August 22, 2024

great, thanks! Whats the ETA for 1.4?

from jsonata.

shrickus avatar shrickus commented on August 22, 2024

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.

andrew-coleman avatar andrew-coleman commented on August 22, 2024

formatNumber() now published in v1.4

from jsonata.

zubairov avatar zubairov commented on August 22, 2024

Awesome, thanks allot!

from jsonata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.