-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (80 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*!
* limon <https://github.com/limonjs/limon>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var lazy = require('./utils')
/**
* > Initialize `Limon` with `input` and `options`.
* Both are completely optional. You can pass plugins
* and tokens to `options`.
*
* **Example**
*
* ```js
* var Limon = require('limon').Limon
* var lexer = new Limon('foo bar')
*
* // or pass only options
* var limon = new Limon({ foo: 'bar' })
* var tokens = limon.tokenize('baz qux')
* ```
*
* @param {String} `input` String value to tokenize, or if object it is assumed `options`.
* @param {Object} `options` Optional options, use it to pass plugins or tokens.
* @api public
*/
function Limon (input, options) {
if (!(this instanceof Limon)) {
return new Limon(input, options)
}
lazy.use(this, {
fn: function (app, opts) {
app.options = lazy.utils.extend(app.options, opts)
}
})
this.defaults(input, options)
this.use(lazy.plugin.prevNext())
}
Limon.prototype.defaults = function defaults (input, options) {
if (lazy.utils.isBuffer(input)) {
input = input.toString('utf8')
}
if (lazy.utils.isObject(input)) {
options = input
input = null
}
this.options = lazy.utils.extend({}, this.options, options)
this.tokens = lazy.utils.isArray(this.tokens) ? this.tokens : []
this.input = input || this.input
this.input = typeof this.input === 'string' ? this.input : null
}
Limon.prototype.tokenize = function tokenize (input, options) {
this.defaults(input, options)
if (!this.input || this.input.length === 0) {
throw new TypeError('limon.tokenize: expect `input` be non-empty string or buffer')
}
var len = this.input.length
var i = 0
while (i < len) {
this.run(this.input[i], i, this.input)
i++
}
return this.tokens
}
/**
* Expose `Limon` instance
*
* @type {Object}
* @api private
*/
module.exports = new Limon()
/**
* Expose `Limon` constructor
*
* @type {Function}
* @api private
*/
module.exports.Limon = Limon