Last active
September 2, 2016 04:34
-
-
Save NickBeeuwsaert/b16fb6ebf9e8a563de0b25c0998fb298 to your computer and use it in GitHub Desktop.
Write your JS objects as plists using JSX!
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
let up = (msg) => new Error(msg); | |
function zip(...args) { | |
args = args.filter(arg => 'length' in args); | |
let result = []; | |
let minLength = Math.min(...args.map(arg => arg.length)); | |
if(args.length === 0) return result; | |
for(let i = 0; i < minLength; i++) { | |
result.push(args.map(arg => arg[i])); | |
} | |
return result; | |
} | |
/** @jsx h */ | |
function h(tag, attr, ...children) { | |
if(!(tag in h.types)) throw up("Bleugh!"); | |
return h.types[tag](tag, attr, ...children); | |
} | |
h.types = { | |
plist(type, _, item, ...items) { | |
return item.value; | |
}, | |
string(type, _, ...children) { | |
return { | |
type, | |
value: children.join('') | |
}; | |
}, | |
integer(type, _, ...children) { | |
return { | |
type, | |
value: parseInt(children.join(''), 10) | |
}; | |
}, | |
real(type, _, ...children) { | |
return { | |
type, | |
value: parseFloat(children.join('')) | |
}; | |
}, | |
date(type, _, ...children) { | |
return { | |
type, | |
value: new Date(children.join('')) | |
}; | |
}, | |
data(type, _, ...children) { | |
return { | |
type, | |
value: atob(children.join('')) | |
}; | |
}, | |
key(type, _, ...children) { | |
return { | |
type, | |
value: children.join('') | |
} | |
}, | |
dict(type, _, ...children) { | |
let keys = children.filter(item => item.type === "key"), | |
items = children.filter(item => item.type !== "key"); | |
let value = zip(keys, items).reduce(function(result, [k, v]) { | |
result[k.value] = v.value; | |
return result; | |
}, {}); | |
return {type, value}; | |
}, | |
array(type, _, ...items) { | |
return { | |
type, | |
value: items.map(item => item.value) | |
}; | |
}, | |
true(type, ..._) { | |
return {type, value: true}; | |
}, | |
false(type, ..._) { | |
return {type, value: false}; | |
} | |
}; | |
console.log(<plist> | |
<array> | |
<dict> | |
<key>Yo</key> | |
<string>Sup</string> | |
<key>Ayyy</key> | |
<array> | |
<string>Sup</string> | |
<string>Yo</string> | |
</array> | |
</dict> | |
<string>ERRY BODY SAY AYYYOOOOO</string> | |
<data>QUFBQUFZIExNQU8K</data> | |
<real>6.9</real> | |
<integer>420</integer> | |
<date>2016-08-29T15:19:12.038Z</date> | |
<dict> | |
<key>is_crunk</key> | |
<true/> | |
<key>has_datboi</key> | |
<false/> | |
</dict> | |
</array> | |
</plist>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment