Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active April 11, 2018 17:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/e55393c96d9081840e9fbdf70fcc0b1b to your computer and use it in GitHub Desktop.
Save toomasv/e55393c96d9081840e9fbdf70fcc0b1b to your computer and use it in GitHub Desktop.
Poor man's format
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
]
]
@toomasv
Copy link
Author

toomasv commented Nov 27, 2017

Examples

Numbers

signed

>> format 99 [signed]
== "+99"
>> format -99 [signed]
== "-99"
>> format 2 / 3 [signed]
== "0"
>> format 20.5 [signed]
== "+20.5"
>> format 8% [signed]
== "+8%"

num

>> format 8.5% [pad/left num 7]
== "   8.5%"
>> format 35 [pad/left/with num 5 #"0"]
== "00035"

round

>> format 35.56789 [round/to num .01]
== "35.57"

sign

>> format 35.56789 [sign round/to num .01]
== "+35.57"

ordinal

>> repeat i 25 [prin [format i [ordinal num space]]]
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 

Strings

>> format "abc" [upper]
== "ABC"
>> format "aBCde" [lower]
== "abcde"
>> format "aBCde" [firstupper]
== "Abcde"
>> format "šEmuEl alKOv" [proper]
== "Šemuel Alkov"

Time

>> print format 1:15:35 ["Hour: " hour lf "Minute: " minute lf "Second: " second]
Hour: 1
Minute: 15
Second: 35.0

Date

>> format now ["Today is " weekday ", " month space ordinal day " of the year " year]
== "Today is Monday, November 27th of the year 2017"
>> format 1-Dec-2017 [dd dot mm dot yy]
== "01.12.17"
>> format 1-Dec-2017 [mon space ordinal day space year]
== "Dec 1st 2017"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment