Code Monkey home page Code Monkey logo

Comments (2)

wanasit avatar wanasit commented on August 16, 2024 1

@zhouzi Sorry for my slow reply. As mentioned in #525, outputting format text for all supported conditions and locales would be challenging.

From what you explained, the problem is you want to recognize the casual reference date (e.g. today, tomorrow) to treat them differently. I think that a reasonable use-case and I found it surprisingly difficult to do it with the current API.

I think this problem could be solved by the parsing tag (#534). Introducing tags is an idea that I have been thinking about for debugging parsers, but I think it also fits your use-case. Could you take a look at #534 and give me some feedback?

from chrono.

zhouzi avatar zhouzi commented on August 16, 2024

This has been solved by #534, here's how our code looks like:

const getCasualText = (parsedComponents: ParsedComponents) => {
  const tags = parsedComponents.tags();
  const translations = [
    ['casualReference/today', 'Today'],
    ['casualReference/yesterday', 'Yesterday'],
    ['casualReference/tomorrow', 'Tomorrow'],
    ['casualReference/now', 'Now'],
  ];

  for (const [tag, casualText] of translations) {
    if (tags.has(tag)) return casualText;
  }

  return undefined;
};

With this function it's possible to convert a parsed date to the appropriate string (Today/Yesterday/Tomorrow/Now). We don't need to translate to other languages but it wouldn't be too hard to do if we ever need it:

const translations = {
  "parser/ENCasualDateParser": [
    ['casualReference/today', 'Today'],
    ['casualReference/yesterday', 'Yesterday'],
    ['casualReference/tomorrow', 'Tomorrow'],
    ['casualReference/now', 'Now'],
  ],
  // ...other parsers
};

It's worth noting that tags are not added for all locales at the time of writing, which is fine for our use case. But if someone needs more tags they might want to send a PR.

from chrono.

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.