refactor: import rollup

This commit is contained in:
harttle
2018-08-20 23:58:41 +08:00
parent 4ece4e208f
commit bc9d8f92af
82 changed files with 8589 additions and 3963 deletions
+2 -4
View File
@@ -1,10 +1,8 @@
const AssertionError = require('./error.js').AssertionError
import {AssertionError} from './error.js'
function assert (predicate, message) {
export default function (predicate, message) {
if (!predicate) {
message = message || `expect ${predicate} to be true`
throw new AssertionError(message)
}
}
module.exports = assert
+9 -9
View File
@@ -1,4 +1,4 @@
import _ from './underscore.js'
import * as _ from './underscore.js'
function initError () {
this.name = this.constructor.name
@@ -14,7 +14,7 @@ function initLiquidError (err, token) {
this.line = token.line
this.file = token.file
let context = mkContext(token.input, token.line)
const context = mkContext(token.input, token.line)
this.message = mkMessage(err.message, token)
this.stack = context +
'\n' + (this.stack || this.message) +
@@ -64,11 +64,11 @@ AssertionError.prototype = Object.create(Error.prototype)
AssertionError.prototype.constructor = AssertionError
function mkContext (input, line) {
let lines = input.split('\n')
let begin = Math.max(line - 2, 1)
let end = Math.min(line + 3, lines.length)
const lines = input.split('\n')
const begin = Math.max(line - 2, 1)
const end = Math.min(line + 3, lines.length)
let context = _
const context = _
.range(begin, end + 1)
.map(l => [
(l === line) ? '>> ' : ' ',
@@ -82,9 +82,9 @@ function mkContext (input, line) {
}
function align (n, max) {
let length = (max + '').length
let str = n + ''
let blank = Array(length - str.length).join(' ')
const length = (max + '').length
const str = n + ''
const blank = Array(length - str.length).join(' ')
return blank + str
}
+3 -8
View File
@@ -1,6 +1,6 @@
const fs = require('fs')
import fs from 'fs'
function readFileAsync (filepath) {
export function readFileAsync (filepath) {
return new Promise(function (resolve, reject) {
fs.readFile(filepath, 'utf8', function (err, content) {
err ? reject(err) : resolve(content)
@@ -8,13 +8,8 @@ function readFileAsync (filepath) {
})
};
function statFileAsync (path) {
export function statFileAsync (path) {
return new Promise(function (resolve, reject) {
fs.stat(path, (err, stat) => err ? reject(err) : resolve(stat))
})
};
module.exports = {
readFileAsync,
statFileAsync
}
+17
View File
@@ -0,0 +1,17 @@
export function get (url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.responseText)
} else {
reject(new Error(xhr.statusText))
}
}
xhr.onerror = () => {
reject(new Error('An error occurred whilst sending the response.'))
}
xhr.open('GET', url)
xhr.send()
})
}
+3 -6
View File
@@ -4,7 +4,7 @@
* @param {Array} iteratee returns a new promise.
* The iteratee is invoked with three arguments: (value, index, iterable).
*/
function anySeries (iterable, iteratee) {
export function anySeries (iterable, iteratee) {
let ret = Promise.reject(new Error('init'))
iterable.forEach(function (item, idx) {
ret = ret.catch(e => iteratee(item, idx, iterable))
@@ -18,9 +18,9 @@ function anySeries (iterable, iteratee) {
* @param {Array} iteratee returns a new promise.
* The iteratee is invoked with three arguments: (value, index, iterable).
*/
function mapSeries (iterable, iteratee) {
export function mapSeries (iterable, iteratee) {
let ret = Promise.resolve('init')
let result = []
const result = []
iterable.forEach(function (item, idx) {
ret = ret
.then(() => iteratee(item, idx, iterable))
@@ -28,6 +28,3 @@ function mapSeries (iterable, iteratee) {
})
return ret.then(() => result)
}
exports.anySeries = anySeries
exports.mapSeries = mapSeries
+21 -23
View File
@@ -1,16 +1,16 @@
let monthNames = [
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
]
let monthNamesShort = [
const monthNamesShort = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec'
]
let dayNames = [
const dayNames = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
]
let dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
let suffixes = {
const dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const suffixes = {
1: 'st',
2: 'nd',
3: 'rd',
@@ -18,9 +18,9 @@ let suffixes = {
}
// prototype extensions
let _date = {
const _date = {
daysInMonth: function (d) {
let feb = _date.isLeapYear(d) ? 29 : 28
const feb = _date.isLeapYear(d) ? 29 : 28
return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
},
@@ -36,21 +36,21 @@ let _date = {
// TODO: that comment was retarted. fix it.
getWeekOfYear: function (d, startDay) {
// Skip to startDay of this week
let now = this.getDayOfYear(d) + (startDay - d.getDay())
const now = this.getDayOfYear(d) + (startDay - d.getDay())
// Find the first startDay of the year
let jan1 = new Date(d.getFullYear(), 0, 1)
let then = (7 - jan1.getDay() + startDay)
const jan1 = new Date(d.getFullYear(), 0, 1)
const then = (7 - jan1.getDay() + startDay)
return _number.pad(Math.floor((now - then) / 7) + 1, 2)
},
isLeapYear: function (d) {
let year = d.getFullYear()
const year = d.getFullYear()
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
},
getSuffix: function (d) {
let str = d.getDate().toString()
let index = parseInt(str.slice(-1))
const str = d.getDate().toString()
const index = parseInt(str.slice(-1))
return suffixes[index] || suffixes['default']
},
@@ -59,7 +59,7 @@ let _date = {
}
}
let _number = {
const _number = {
pad: function (value, size, ch) {
if (!ch) ch = '0'
let result = value.toString()
@@ -73,7 +73,7 @@ let _number = {
}
}
let formatCodes = {
const formatCodes = {
a: function (d) {
return dayNamesShort[d.getDay()]
},
@@ -162,7 +162,7 @@ let formatCodes = {
return d.getFullYear()
},
z: function (d) {
let tz = d.getTimezoneOffset() / 60 * 100
const tz = d.getTimezoneOffset() / 60 * 100
return (tz > 0 ? '-' : '+') + _number.pad(Math.abs(tz), 4)
},
'%': function () {
@@ -172,13 +172,13 @@ let formatCodes = {
formatCodes.h = formatCodes.b
formatCodes.N = formatCodes.L
let strftime = function (d, format) {
export default function (d, format) {
let output = ''
let remaining = format
while (true) {
let r = /%./g
let results = r.exec(remaining)
const r = /%./g
const results = r.exec(remaining)
// No more format codes. Add the remaining text and return
if (!results) {
@@ -190,10 +190,8 @@ let strftime = function (d, format) {
remaining = remaining.slice(r.lastIndex)
// Add the format code
let ch = results[0].charAt(1)
let func = formatCodes[ch]
const ch = results[0].charAt(1)
const func = formatCodes[ch]
output += func ? func.call(this, d) : '%' + ch
}
}
module.exports = strftime
+19 -36
View File
@@ -5,11 +5,11 @@ const toStr = Object.prototype.toString
* @param {any} value The value to check.
* @return {Boolean} Returns true if value is a string, else false.
*/
function isString (value) {
export function isString (value) {
return toStr.call(value) === '[object String]'
}
function stringify (value) {
export function stringify (value) {
if (isNil(value)) {
return String(value)
}
@@ -23,7 +23,7 @@ function stringify (value) {
return value
}
let cache = []
const cache = []
return JSON.stringify(value, (key, value) => {
if (isObject(value)) {
if (cache.indexOf(value) !== -1) {
@@ -35,17 +35,17 @@ function stringify (value) {
})
}
function isNil (value) {
export function isNil (value) {
return value === null || value === undefined
}
function isArray (value) {
export function isArray (value) {
// be compatible with IE 8
return toStr.call(value) === '[object Array]'
}
function isError (value) {
let signature = Object.prototype.toString.call(value)
export function isError (value) {
const signature = Object.prototype.toString.call(value)
// [object XXXError]
return signature.substr(-6, 5) === 'Error' ||
(typeof value.message === 'string' && typeof value.name === 'string')
@@ -59,9 +59,9 @@ function isError (value) {
* @param {Function} iteratee The function invoked per iteration.
* @return {Object} Returns object.
*/
function forOwn (object, iteratee) {
export function forOwn (object, iteratee) {
object = object || {}
for (let k in object) {
for (const k in object) {
if (object.hasOwnProperty(k)) {
if (iteratee(object[k], k, object) === false) break
}
@@ -80,20 +80,20 @@ function forOwn (object, iteratee) {
* @param {...Object} sources The source objects.
* @return {Object} Returns object.
*/
function assign (object) {
export function assign (object) {
object = isObject(object) ? object : {}
let srcs = Array.prototype.slice.call(arguments, 1)
const srcs = Array.prototype.slice.call(arguments, 1)
srcs.forEach((src) => Object.assign(object, src))
return object
}
function last (arr) {
export function last (arr) {
return arr[arr.length - 1]
}
function uniq (arr) {
let u = {}
let a = []
export function uniq (arr) {
const u = {}
const a = []
for (let i = 0, l = arr.length; i < l; ++i) {
if (u.hasOwnProperty(arr[i])) {
continue
@@ -110,8 +110,8 @@ function uniq (arr) {
* @param {any} value The value to check.
* @return {Boolean} Returns true if value is an object, else false.
*/
function isObject (value) {
let type = typeof value
export function isObject (value) {
const type = typeof value
return value != null && (type === 'object' || type === 'function')
}
@@ -123,33 +123,16 @@ function isObject (value) {
* Note that ranges that stop before they start are considered to be zero-length instead of
* negative — if you'd like a negative range, use a negative step.
*/
function range (start, stop, step) {
export function range (start, stop, step) {
if (arguments.length === 1) {
stop = start
start = 0
}
step = step || 1
let arr = []
const arr = []
for (let i = start; i < stop; i += step) {
arr.push(i)
}
return arr
}
// lang
exports.isString = isString
exports.isObject = isObject
exports.isArray = isArray
exports.isNil = isNil
exports.isError = isError
// array
exports.range = range
exports.last = last
// object
exports.forOwn = forOwn
exports.assign = assign
exports.uniq = uniq
exports.stringify = stringify
+20 -4
View File
@@ -1,5 +1,4 @@
import resolveUrl from 'resolve-url'
import _ from './underscore'
import {last, isArray} from './underscore'
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/
const urlRe = /^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$/
@@ -15,11 +14,28 @@ export function valid (path) {
}
export function resolve (root, path) {
if (Object.prototype.toString.call(root) === '[object Array]') {
if (isArray(root)) {
root = root[0]
}
if (root && _.last(root) !== '/') {
if (root && last(root) !== '/') {
root += '/'
}
return resolveUrl(root, path)
}
function resolveUrl (root, path) {
const base = document.createElement('base')
base.href = arguments[0]
const head = document.getElementsByTagName('head')[0]
head.insertBefore(base, head.firstChild)
const a = document.createElement('a')
a.href = path
const resolved = a.href
base.href = resolved
head.removeChild(base)
return resolved
}