当前位置: 动力学知识库 > 问答 > 编程问答 >

How do I write Meteor packages in Coffeescript?

问题描述:

I use CoffeeScript [CS] /heavily/ in my meteor sources. In fact, everything in my project is written using CS. I want to write packages by the same token. How should they be organized, declared, and written so they leverage the power of the CS dialect while maximizing testability and portability?

网友答案:

In short, you need only api.use('coffeescript'); in your Package.onUse and Package.onTest in order to write your packages in CoffeeScript. See the docs for an outline of the namespacing quirks.

Here's a simple example of a package called safe which contains the following four files:

package.js

Package.describe({
  name: 'safe',
  summary: 'Encrypt strings to keep them safe (or not)'
});

Package.onUse(function(api) {
  api.versionsFrom('1.1.0.3');
  api.export('Safe');
  api.use('coffeescript');
  api.addFiles('encrypt.coffee');
  api.addFiles('safe.coffee');
});

Package.onTest(function(api) {
  api.use('tinytest');
  api.use('safe');
  api.use('coffeescript');
  api.addFiles('tests.coffee');
});

encrypt.coffee

# use the share object to export code to other files in the package
share.encrypt = (string) ->
  # a super strong encryption :)
  string.replace /[a-zA-Z]/g, (c) ->
    String.fromCharCode (if ((if c <= "Z" then 90 else 122)) >= (c = c.charCodeAt(0) + 13) then c else c - 26)

safe.coffee

{encrypt} = share

class Safe
  constructor: (@string) ->

  encrypt: ->
    encrypt @string

tests.coffee

Tinytest.add 'safe encryption', (test) ->
  safe = new Safe 'pandapants'
  test.equal safe.encrypt(), 'cnaqncnagf'

This should give you a template to start from. If you need additional clarification, just ask in the comments and I'll update the answer as needed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: