javascript - Representing Ranges of Time with Different Units in a Comparable Way -
i have event objects have lifespan attribute. have varying types of events, each different lifespans , potentially different units (e.g. 4-10 hours, 5-8 weeks, 1 month - 2 years). want store these ranges uniform , comparable datatype i'm not sure best option is. ideally want able go through list of events , find can last 3 hours, 1 week, 2 months, etc.
the problem having comparing values same "substance" (i.e., time) yet have different units. great candidate value object. value objects not common pattern in javascript, no less applicable , great place domain logic. this article great @ using value objects javascript.
create "class" defining javascript function.
in it, store value lifespan in smallest units use (hours).
create several internal methods , set value based on unit type (for example, multiplying weeks 168 number of hours). may want round nearest week/month/year when getting in units.
create methods compare value in hours value in hours of instance.
this allow compare events independently of storing unit of time lifespan is.
Comments
Post a Comment