简体   繁体   中英

Going from JSON to generated CoffeeScript

I have currently hard-coded in some data for a demo of a project. The code looks like this:

update: () =>
    t = getTime()
    if t == undefined
        zone = -1
    else if between(t, 100, 110) 
        zone = 1
    else if between(t, 111, 120) 
        zone = 2

Now, the next step is to take a json array of the form:

[{"start": 100, "end": 110, "zone": 1}, {"start": 111, "end": 120,"zone": 2}]

to make the generated code above. Is this possible? What is the best way to go about doing this?

fs = require 'fs'

json = fs.readFileSync 'PUT_FILENAME_HERE', 'utf-8'
data = JSON.parse json

for zoneData in data
  if between(t, zoneData.start, zoneData.end)
    zone = zoneData.zone
    break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM