Anonymous user
Simple database: Difference between revisions
no edit summary
(Added Erlang as script) |
No edit summary |
||
Line 3,073:
Category: bar
Date: Tue Nov 15 18:12:07 GMT 2011</lang>
=={{header|ToffeeScript}}==
<lang coffeescript>#!/usr/local/bin/toffee
prog = require 'commander'
fs = require 'fs-extra'
if not fs.exists! 'data.json'
fs.outputJson! 'data.json', {}
prog
.command('add <name> <category> [date]')
.description('Add a new entry')
.option('-n <text>', 'notes')
.option('-t <tags>', 'tags')
.action addentry
prog
.command('latest')
.description('Print the latest entry')
.action latest
prog
.command('catlatest')
.description('Print the latest entry for each category')
.action catlatestout
prog
.command('list')
.description('Print all entries sorted by date')
.action bydate
addentry = (name, category, dt, options) ->
if dt? then dat = new Date(dt) else dat = new Date()
update =
name: name
category: category
tags: options?.T
notes: options?.N
date: dat.getTime()
e, data = fs.readJson! 'data.json'
if not data[category]?
data[category] = []
data[category].push update
fs.outputJson 'data.json', data
byDateNew = (a, b) ->
if a.date<b.date then return 1
if b.date>a.date then return -1
return 0
catlatest = (cb) ->
e, data = fs.readJson! 'data.json'
ret = []
for cat, list of data
list.sort byDateNew
ret.push list[0]
cb ret</lang>
=={{header|UNIX Shell}}==
|