Last active
April 11, 2018 17:53
-
-
Save toomasv/e55393c96d9081840e9fbdf70fcc0b1b to your computer and use it in GitHub Desktop.
Poor man's format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Author: "Toomas Vooglaid" | |
Date: 27-11-2017 | |
] | |
context [ | |
tmp: x: none | |
ord: func [val /local v][ | |
rejoin [ | |
v: to-string val | |
either find [11 12 13] val ["th"][ | |
switch/default last v [ | |
#"1" ["st"] | |
#"2" ["nd"] | |
#"3" ["rd"] | |
]["th"] | |
] | |
] | |
] | |
fca: make reactor! [ | |
val: 00:00:01 | |
hour: is [val/hour] | |
minute: is [val/minute] | |
second: is [val/second] | |
num: 0 | |
sign: func [val][either num <= 0 [val][rejoin [#"+" val]]] | |
signed: is [either num <= 0 [num][rejoin [#"+" num]]] | |
str: "" | |
upper: is [uppercase copy str] | |
lower: is [lowercase copy str] | |
firstupper: is [ | |
unless empty? str [ | |
tmp: lowercase copy str | |
tmp/1: uppercase tmp/1 | |
copy tmp | |
] | |
] | |
proper: is [ | |
tmp: lowercase copy str | |
parse tmp [ | |
change copy x skip (uppercase x) | |
any [thru #" " change copy x skip (uppercase x)] | |
] | |
tmp | |
] | |
ordinal: :ord | |
] | |
fcd: make reactor! [ | |
val: 1-1-2017 | |
date: is [val/date] | |
year: is [val/year] | |
yy: is [copy skip to-string val/year 2] | |
month: is [pick system/locale/months val/month] | |
mon: is [copy/part pick system/locale/months val/month 3] | |
mm: is [pad/left/with val/month 2 #"0"] | |
m: is [val/month] | |
day: is [val/day] | |
dd: is [pad/left/with val/day 2 #"0"] | |
zone: is [val/zone] | |
time: is [val/time] | |
hour: is [val/hour] | |
minute: is [val/minute] | |
second: is [val/second] | |
weekday: is [pick system/locale/days val/weekday] | |
wd: is [val/weekday] | |
yearday: is [val/yearday] | |
timezone: is [val/timezone] | |
week: is [val/week] | |
isoweek: is [val/isoweek] | |
ordinal: :ord | |
] | |
set 'format func [value frmt][ | |
case [ | |
date? value [fcd/val: value bind frmt fcd] | |
time? value [fca/val: value bind frmt fca] | |
number? value [fca/num: value bind frmt fca] | |
string? value [fca/str: value bind frmt fca] | |
] | |
rejoin frmt | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples
Numbers
signed
num
round
sign
ordinal
Strings
Time
Date