Coder Social home page Coder Social logo

Allow implied past or future about chrono HOT 3 CLOSED

wanasit avatar wanasit commented on August 18, 2024
Allow implied past or future

from chrono.

Comments (3)

HaNdTriX avatar HaNdTriX commented on August 18, 2024 1

The code above doesn't work anymore since the api has changed.
Just in case somebody has the same issue, here my refiner that should work with the latest version (1.0.x).

function isPastDate({year, month, day}, ref) {
  let refDay = ref.getDate();
  let refMonth = ref.getMonth() + 1;
  let refYear = ref.getFullYear();
  if(refYear > year) {
    return true;
  }
  if(refMonth > month){
    return true;
  }
  if(refDay > day){
    return true;
  }
  return false;
}

var PreferPastLastWeek = new chrono.Refiner();
PreferPastLastWeek.refine = function(text, results) {
  results.forEach(function(result) {
    if (result.start.isCertain('weekday') && !result.start.isCertain('day')) {
      if(isPastDate(result.start.impliedValues, result.ref)) {
        result.start.imply('day', result.start.impliedValues.day + 7)
      }
    }
  });
  return results;
};

chrono.casual.refiners.push(PreferPastLastWeek);
chrono.strict.refiners.push(PreferPastLastWeek);

Anyway thanks @wanasit for this awesome lib!

from chrono.

wanasit avatar wanasit commented on August 18, 2024

Actually, you can customize Chrono with some plug-in called refiners.
(It's currently undocumented. Sorry that I didn't have time to do that recently)

var chrono = require('chromo');
var _chrono = new chrono();  //Let's keep the global chrono uncustomized

_chrono.parseDate('Thursday', new Date(2013,11,2))
// Thu Dec 05 2013 12:00:00 GMT+0900 (JST)

_chrono.refiners.PreferPastLastWeek = {
    refine: function(text, results) {
        results.forEach(function(result){
            // Check and modify the result
            if( result.start.isCertain('dayOfWeek') && !result.start.isCertain('day')){
                result.start.imply('day', result.start.day - 7)
            }
        })
        return results;
    }
}

_chrono.parseDate('Thursday', new Date(2013,11,2))
// Thu Nov 28 2013 12:00:00 GMT+0900 (JST)

You can define plug-in chrono.refiners.XXX with an object with function refine (receives text and an array of parsed results as arguments, and returns array of modified results). As its name, you can refine the parsing results with your custom rules.

Hope this helps

from chrono.

hazeledmands avatar hazeledmands commented on August 18, 2024

Right on! Can't wait to try it out :) 👍

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.