Skip to content

Commit

Permalink
helpers: now directly exported
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Sep 25, 2010
1 parent 9005682 commit e0ed254
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fs = require 'fs'
{helpers} = require './lib/helpers'
helpers = require './lib/helpers'
CoffeeScript = require './lib/coffee-script'
{spawn, exec} = require 'child_process'
path = require 'path'
Expand Down
6 changes: 3 additions & 3 deletions bin/cake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

var path = require('path');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');

require(lib + '/helpers').helpers.extend(global, require('sys'));
require(lib + '/helpers').extend(global, require('sys'));
require(lib + '/cake').run();
6 changes: 3 additions & 3 deletions bin/coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

var path = require('path');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');

require(lib + '/helpers').helpers.extend(global, require('sys'));
require(lib + '/helpers').extend(global, require('sys'));
require(lib + '/command').run();
2 changes: 1 addition & 1 deletion lib/cake.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions lib/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function() {
var CoffeeScript, helpers, readline, repl, run, stdio;
CoffeeScript = require('./coffee-script');
helpers = require('./helpers').helpers;
helpers = require('./helpers');
readline = require('readline');
stdio = process.openStdin();
helpers.extend(global, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/scope.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cake.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# External dependencies.
fs = require 'fs'
path = require 'path'
helpers = require('./helpers').helpers
helpers = require './helpers'
optparse = require './optparse'
CoffeeScript = require './coffee-script'

Expand Down
2 changes: 1 addition & 1 deletion src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fs = require 'fs'
path = require 'path'
optparse = require './optparse'
CoffeeScript = require './coffee-script'
{helpers} = require './helpers'
helpers = require './helpers'
{spawn, exec} = require 'child_process'
{EventEmitter} = require 'events'

Expand Down
22 changes: 10 additions & 12 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
# arrays, count characters, that sort of thing.

helpers = exports.helpers = {}

# Cross-engine indexOf, so that JScript can join the party.
indexOf = helpers.indexOf = Array.indexOf or
indexOf = exports.indexOf = Array.indexOf or
if Array::indexOf
(array, item, from) -> array.indexOf item, from
else
Expand All @@ -16,45 +14,45 @@ indexOf = helpers.indexOf = Array.indexOf or
-1

# Does a list include a value?
helpers.include = (list, value) -> 0 <= indexOf list, value
exports.include = (list, value) -> 0 <= indexOf list, value

# Peek at the beginning of a given string to see if it matches a sequence.
helpers.starts = (string, literal, start) ->
exports.starts = (string, literal, start) ->
literal is string.substr start, literal.length

# Peek at the end of a given string to see if it matches a sequence.
helpers.ends = (string, literal, back) ->
exports.ends = (string, literal, back) ->
ll = literal.length
literal is string.substr string.length - ll - (back or 0), ll

# Trim out all falsy values from an array.
helpers.compact = (array) -> item for item in array when item
exports.compact = (array) -> item for item in array when item

# Count the number of occurences of a character in a string.
helpers.count = (string, letter) ->
exports.count = (string, letter) ->
num = pos = 0
num++ while 0 < pos = 1 + string.indexOf letter, pos
num

# Merge objects, returning a fresh copy with attributes from both sides.
# Used every time `BaseNode#compile` is called, to allow properties in the
# options hash to propagate down the tree without polluting other branches.
helpers.merge = (options, overrides) ->
exports.merge = (options, overrides) ->
extend (extend {}, options), overrides

# Extend a source object with the properties of another object (shallow copy).
# We use this to simulate Node's deprecated `process.mixin`
extend = helpers.extend = (object, properties) ->
extend = exports.extend = (object, properties) ->
(object[key] = val) for all key, val of properties
object

# Return a flattened version of an array (nonrecursive).
# Handy for getting a list of `children` from the nodes.
helpers.flatten = (array) -> array.concat.apply [], array
exports.flatten = (array) -> array.concat.apply [], array

# Delete a key from an object, returning the value. Useful when a node is
# looking for a particular method in an options hash.
helpers.del = (obj, key) ->
exports.del = (obj, key) ->
val = obj[key]
delete obj[key]
val
2 changes: 1 addition & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{Rewriter} = require './rewriter'

# Import the helpers we need.
{include, count, starts, compact} = require('./helpers').helpers
{include, count, starts, compact} = require './helpers'

# The Lexer Class
# ---------------
Expand Down
2 changes: 1 addition & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{Scope} = require './scope'

# Import the helpers we plan to use.
{compact, flatten, merge, del, include, indexOf, starts, ends} = require('./helpers').helpers
{compact, flatten, merge, del, include, indexOf, starts, ends} = require './helpers'

#### BaseNode

Expand Down
2 changes: 1 addition & 1 deletion src/repl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Require the **coffee-script** module to get access to the compiler.
CoffeeScript = require './coffee-script'
helpers = require('./helpers').helpers
helpers = require './helpers'
readline = require 'readline'

# Start by opening up **stdio**.
Expand Down
2 changes: 1 addition & 1 deletion src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# parentheses, balance incorrect nestings, and generally clean things up.

# Import the helpers we need.
{include} = require('./helpers').helpers
{include} = require './helpers'

# The **Rewriter** class is used by the [Lexer](lexer.html), directly against
# its internal array of tokens.
Expand Down
2 changes: 1 addition & 1 deletion src/scope.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# with the outside.

# Import the helpers we plan to use.
{extend} = require('./helpers').helpers
{extend} = require './helpers'

exports.Scope = class Scope

Expand Down
2 changes: 1 addition & 1 deletion test/test_helpers.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ indexOf, include, starts, ends, compact, count, merge, extend, flatten, del
} = require('../lib/helpers').helpers
} = require '../lib/helpers'

array = [0..4]

Expand Down

0 comments on commit e0ed254

Please sign in to comment.