livescript 按折扣价格排序

sortByDiscount
const { sortBy, orderBy } = require('lodash');

const filteredGames = [
  { name: 'game2', discountActive: 1, price: 5, priceDiscounted: 4 },
  { name: 'game6', discountActive: 1, price: 20, priceDiscounted: 8 },
  { name: 'game5', discountActive: 0, price: 10, priceDiscounted: 9 },
  { name: 'game3', discountActive: 0, price: 11, priceDiscounted: 10 },
  { name: 'game4', discountActive: 1, price: 22, priceDiscounted: 17 },
  { name: 'game1', discountActive: 0, price: 21, priceDiscounted: 18 },
];

const value = game => game.discountActive === 1 ? game.priceDiscounted : game.price;
const sorted = filteredGames.sort((a, b) => value(a) - value(b));

console.log(sorted);

livescript 获取和设置

https://ruepprich.wordpress.com/2013/01/11/getting-and-setting-apex-page-item-values-using-v-s-v2/

txt
For example, if you wante to access the contents of a text field with JavaScript, would would need to reference it something like this:

$x("P2_TEXT_FIELD").value;
If the item you want to reference is a display only item, then the syntax changes:

x = $("#P2_DISPLAY_ONLY").text();
If you need to set the values of these items, the you need to use the varying syntax as well:

$x("P2_TEXT_FIELD").value = "Hello World!";
or

$("#P2_DISPLAY_ONLY").text("Hello World!");
Dealing with these various syntax constructs can be confusing. Fortunately the Apex JavaScript API makes this process much easier. To get page item values simply use $v(“<item_name>”):

x = $v("P2_TEXT_FIELD");
y = $v("P2_DISPLAY_ONLY");



To set the item values use:

$s("P2_TEXT_FIELD","Hello World!");
$s("P2_DISPLAY_ONLY","Hello World!");




myArr = $v2("P2_SHUTTLE_CONTROL");
for (idx=0; idx<myArr.length; idx++) {
  //do something with myArr[idx];
}

livescript 我的资源加载器

我的资源加载器

loader.ls
base = "http://z.tyio.net:8001/zip/"


@loadResource = (name, callback) ->
    i = name.indexOf "#"
    if i > 0
        path = name.slice 0, i
        key = name.slice i + 1
    else
        path = name
        key = null

    res = resources[path]

    run = (res) ->
        if key
            [x, y, w, h] = res.meta[key]
            res.zip.file key .async \uint8array
                .then (data) ->
                    ok =
                        x: x
                        y: y
                        w: w
                        h: h
                        data: data
                    callback ok
        else
            'blah...'

    if not res
        res = resources[path] = []
        JSZipUtils.getBinaryContent "#base#path", (err, data) ->
            if err
                resources[path] = null
                throw err
            JSZip.loadAsync data
            .then (zip) ->
                zip.file \_ .async \string
                    .then (meta) ->
                        res =
                            meta: JSON.parse(meta)
                            zip: zip
                        for run in resources[path]
                            run(res)
                        resources[path] = res

    if res.push  # downloading, push into queue
        res.push run
    else  # downloaded
        run res

livescript 用LiveScript编写的时间和日期格式化函数

用LiveScript编写的时间和日期格式化函数

aux.time_formatting.ls
aux =
    # formatting
    timezoneOffset: new Date().getTimezoneOffset!
    getTime: (t = Date.now!) !->
        return new Date(t - timezoneOffset *60_000min_to_ms).toISOString! .replace(/.+?T|\..+/g, '')
    getDateTime: (t = Date.now!) !->
        return new Date(t - timezoneOffset *60_000min_to_ms).toISOString! .replace(/T|\..+/g, ' ')
    getDate: (t = Date.now!) !->
        return new Date(t - timezoneOffset *60_000min_to_ms).toISOString! .replace(/T.+/g, '')
    getISOTime: (t = new Date)!->
        return t.toISOString! .replace(/T|\..+/g, " ")
    parseISOTime: (t) !->
        return new Date(t) - timezoneOffset *60_000min_to_ms

    # show a timespan (in ms) in a human friendly format (e.g. "2 hours")
    humanTime: (diff, shortFormat) !->
        if diff < 0
            return "-#{humanTime -diff}"
        else if not shortFormat and diff < 2_000ms # keep in sync with ago()
            return "just now"
        b=[60to_min, 60to_h, 24to_days, 360.25to_years]; c=0
        diff /= 1000to_s
        while diff > 2*b[c] then diff /= b[c++]
        if shortFormat
            return "#{Math.round diff}#{<[ s m h d y ]>[c]}"
        else
            return "#{Math.round diff} #{<[ seconds minutes hours days years ]>[c]}"

    # show a timespan (in s) in a format like "mm:ss" or "hh:mm:ss" etc
    mediaTime: (dur) !->
        return "-#{mediaTime -dur}" if dur < 0
        # usually the user would rather read 580 hours as "580:00:00" instead of "24:04:00:00"
        m=0
        if dur >= 60
            m = ~~(dur / 60); dur %= 60
            if m >= 60
                h = ~~(m / 60); m %= 60
        return "#{if h then pad(h)+":" else ''}#{pad(m)}:#{pad(~~dur)}"

    # create string saying how long ago a given timestamp (in ms since epoche) is
    ago: (d) !->
        d = Date.now! - d
        if d < 2_000ms # keep in sync with humanTime()
            return "just now"
        else
            return "#{humanTime(d)} ago"

livescript Splatting.ls

Splatting.ls
posts['op' ...'replies'] = thread



# even with objects
extended = {...base, +extended}

livescript Extended-parameters.ls

Extended-parameters.ls
class Person
  # defaults @name to [], and assigns the first element
  set-first-name: (@[]names.0) ->
  
  # defaults @name to [], and adds the element (* being the array's length)
  add-name: (@[]names[*]) ->
  
  # defaults @hair to {}, and assigns color
  set-hair-color: (@{}hair.color) ->







p = new Person
p.hair # undefined
p.set-hair-color "#8b0000"
p.hair.color # "#8b0000"

p.names # undefined
p.add-name 'George'
p.add-name 'Jean'
p.names # ["George", "Jean"]
p.set-first-name "Paul"
p.names # ["Paul", "Jean"]

livescript Destructuring-defaults.ls

Destructuring-defaults.ls
{username ? 'root', password || '', secure && 'https' 
|| 'http'} = config<[ USERNAME PASSWORD SECURE ]>

livescript dynamic-key.ls

dynamic-key.ls
foo = 'key'
bar = {(foo): 5, "dyna#foo": 6} #=> {'key': 5, 'dynakey': 6}

# in destructuring
{(+* .>>. 1): middle} = [1 to 7] # middle = 4

livescript context-with-new.ls

context-with-new.ls
paul = new
  @name = 'paul'
  @age = 25

# LiveScript has real object comprehensions
flip = (obj) -> new
  for own k, v of obj then @[v] = k

livescript dynamic-flagging.ls

dynamic-flagging.ls
//
  my?
  #normal
  #{[interpolation]}
  #flag
//?