import parsers from './parsers' ; import refiners from './refiners' ; import { strftimeAssigners , defaultAssigners } from './assigners' ; import Token from './parsers/Token' ; import { Date , Format , ParsedResult , } from './types' ; export default class Guesser { constructor ( ) { } static parse ( date ) { const parsedResults = [ ] ; parsers . forEach ( parser => { const parsedResult = parser . parse ( date ) ; if ( parsedResult ) { parsedResults . push ( { ... parsedResult } ) ; } } ) ; return parsedResults ; } static refine ( parsedResults ) { let refinedParsedResults = [ ... parsedResults ] ; refiners . forEach ( refiner => { refinedParsedResults = [ ... refiner . refine ( refinedParsedResults ) ] ; } ) ; return refinedParsedResults ; } static assign ( tokens , format ? ) { let assigners = ( ! format || format === 'default' ) ? defaultAssigners : strftimeAssigners ; assigners . forEach ( assigner => { tokens . forEach ( token => { assigner . assign ( token ) ; } ) ; } ) ; } static getFormatString ( tokens ) { let formatString = '' ; tokens . forEach ( token => { if ( token . format === 'NA' ) { throw Error ( `Couldn't find strftime modifier for "${ token . value }"` ) ; } formatString += token . format ? token . format : token . value ; } ) ; return formatString ; } }