mirror of
https://github.com/flynx/pWiki.git
synced 2025-10-29 18:10:09 +00:00
testing arg parser...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
767f81e7c2
commit
ae13b7de51
40
pwiki2.js
40
pwiki2.js
@ -1780,6 +1780,46 @@ module.pwiki =
|
|||||||
Page('/', '/', store)
|
Page('/', '/', store)
|
||||||
|
|
||||||
|
|
||||||
|
var parseArgs = function(spec, args){
|
||||||
|
// spec...
|
||||||
|
var order = spec.slice()
|
||||||
|
var bools = new Set(
|
||||||
|
order[order.length-1] instanceof Array ?
|
||||||
|
order.pop()
|
||||||
|
: [])
|
||||||
|
order = order
|
||||||
|
.filter(function(k){
|
||||||
|
return !(k in args) })
|
||||||
|
|
||||||
|
var res = {}
|
||||||
|
var pos = Object.entries(args)
|
||||||
|
// stage 1: populate res with explicit data and place the rest in pos...
|
||||||
|
.reduce(function(pos, [key, value]){
|
||||||
|
/^[0-9]+$/.test(key) ?
|
||||||
|
(bools.has(value) ?
|
||||||
|
// bool...
|
||||||
|
(res[value] = true)
|
||||||
|
// positional...
|
||||||
|
: (pos[key*1] = value))
|
||||||
|
// keyword...
|
||||||
|
: (res[key] = value)
|
||||||
|
return pos }, [])
|
||||||
|
// stage 2: populate implicit values from pos...
|
||||||
|
.forEach(function(e, i){
|
||||||
|
order.length == 0 ?
|
||||||
|
(res[e] = true)
|
||||||
|
: (res[order.shift()] = e) })
|
||||||
|
return res }
|
||||||
|
|
||||||
|
console.log('---',
|
||||||
|
parseArgs(
|
||||||
|
['src', 'bam', ['first', 'second']],
|
||||||
|
{1: 'first', 2: '..', src2: 'second', moo: 'third'}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// XXX experiments and testing...
|
// XXX experiments and testing...
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user