Skip to content

Commit

Permalink
Extending FuzzyTime props with native span props (#118)
Browse files Browse the repository at this point in the history
* Extending props with Native Span props

Pass through span props to allow implementor to extend FuzzyTime with
native span props

* got some feedback that comments might be helpful here
  • Loading branch information
Nathan Stilwell authored Sep 21, 2020
1 parent a1f0dce commit 8bf7d23
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 15 additions & 5 deletions packages/ui-components/src/FuzzyTime/FuzzyTime.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import React, { FunctionComponent } from "react";
import React, { FunctionComponent, HTMLAttributes } from "react";
import classnames from "classnames/bind";

import { fuzzy } from "./util";
import styles from "./FuzzyTime.module.scss";

export interface FuzzyTimeProps {
type OwnFuzzyProps = {
timestamp: Date;
}
};

export type FuzzyTimeProps = OwnFuzzyProps &
React.HTMLAttributes<HTMLSpanElement>;

const cx = classnames.bind(styles);

export const FuzzyTime: FunctionComponent<FuzzyTimeProps> = ({ timestamp }) => {
export const FuzzyTime: FunctionComponent<FuzzyTimeProps> = ({
timestamp,
...rest
}) => {
const timeago = fuzzy(timestamp);
const classnames = cx("fuzzy-time");

return <span className={classnames}>{timeago}</span>;
return (
<span className={classnames} {...rest}>
{timeago}
</span>
);
};

export * from "./util";
Expand Down
18 changes: 15 additions & 3 deletions packages/ui-components/src/FuzzyTime/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { aYear, aMonth, aDay, anHour, aMinute, aSecond } from "./constants";

// A time unit represents a relative time unit to display time in (seconds, hours, years, etc)
export type TimeUnit = {
amount: number;
comparator: (x: number) => boolean;
template: (d: number) => string;
amount: number; // the amount of time the unit represents (number of milliseconds)
comparator: (x: number) => boolean; // a function to to decide if this unit should be used or not
template: (d: number) => string; // a string template used to display time unit given number of milliseconds
};

export const gteq = (x: number, y: number): boolean => x >= y;
Expand Down Expand Up @@ -59,12 +60,23 @@ export const TimeUnits: Array<TimeUnit> = [
},
];

// fuzzyFormatter will return a formatting function given an offset
// Usage:
// const formatter = fuzzyFormatter(numberOfMilliseconds);
// const fuzzyTimeString = formatter(timeUnit);
//
export const fuzzyFormatter = (offset: number) => (unit: TimeUnit) => {
if (unit === undefined || offset === undefined) return "";
const duration = Math.floor(offset / unit.amount);
return unit.template(duration);
};

// setFuzzy takes a date representing "now" and returns a function that will format
// a given date in a "time ago" form.
// Usage:
// const getFuzzyTime = setFuzzy(now);
// const fuzzyTime = getFuzzyTime(someDate);
//
export const setFuzzy = (now: Date) => (timedate: Date) => {
const diff = now.getTime() - timedate.getTime();
const format = fuzzyFormatter(diff);
Expand Down

0 comments on commit 8bf7d23

Please sign in to comment.