Skip to content

Instantly share code, notes, and snippets.

@ceremcem
Last active March 3, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceremcem/4ca8d774e660c579682839620d8c2da8 to your computer and use it in GitHub Desktop.
Save ceremcem/4ca8d774e660c579682839620d8c2da8 to your computer and use it in GitHub Desktop.
# Run this script at http://livescript.net
export function merge _obj1, obj2, inplace=no
obj1 = unless inplace
JSON.parse JSON.stringify _obj1
else
_obj1
for p of obj2
t-obj1 = typeof! obj1[p]
if typeof! obj2[p] is \Object
if t-obj1 is \Object
merge obj1[p], obj2[p], yes
else
obj1[p] = obj2[p]
else
if t-obj1 is \Array
# array, merge with current one
for i, j of obj2[p]
if obj1[p].index-of(j) is -1
obj1[p] ++= j
else if obj2[p] isnt void
obj1[p] = obj2[p]
else
delete obj1[p]
return obj1
config =
default:
ip: \192.168.1.5
port: 2000
name: \root
password: \root
db:
name: \default
location: \LA
foo-database:~ -> @default `merge` do
ip: \11.11.11.11
db:
name: \my-foo
bar-database:~ -> @foo-database `merge` do
password: \1234
db:
location: \SF
baz-database:~ -> @foo-database `merge` do
password: 'my-baz'
config.default
# => {"ip":"192.168.1.5","port":2000,"name":"root","password":"root","db":{"name":"default","location":"LA"}}
config.foo-database
# => {"ip":"11.11.11.11","port":2000,"name":"root","password":"root","db":{"name":"my-foo","location":"LA"}}
config.bar-database
# => {"ip":"11.11.11.11","port":2000,"name":"root","password":"1234","db":{"name":"my-foo","location":"SF"}}
config.baz-database
# => {"ip":"11.11.11.11","port":2000,"name":"root","password":"my-baz","db":{"name":"my-foo","location":"LA"}}
@sourcevault
Copy link

This is not a configuration file, it's a program 😺.

configuration files are the buttons in a elevators.

programs are the internal motors / actuators / micro-controllers.

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