[Pkg-javascript-commits] [node-tosource] 01/02: Imported Upstream version 1.0.0

Jérémy Lal kapouer at moszumanska.debian.org
Thu Nov 5 12:08:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

kapouer pushed a commit to branch master
in repository node-tosource.

commit 21c85d1bfc02a1c84a69767eab1a2448c6a0c01f
Author: Jérémy Lal <kapouer at melix.org>
Date:   Thu Nov 5 12:52:20 2015 +0100

    Imported Upstream version 1.0.0
---
 .gitignore   |   1 +
 .travis.yml  |   8 +++++
 HISTORY.md   |  17 ++++++++++
 LICENSE      |  20 ++++++++++++
 Readme.md    | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json |  26 +++++++++++++++
 test.js      |  97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tosource.js  |  77 ++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 348 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..c0901e2
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+sudo: false
+language: node_js
+node_js:
+    - "0.10"
+    - 0.12
+    - 2
+    - 3
+    - iojs
diff --git a/HISTORY.md b/HISTORY.md
new file mode 100644
index 0000000..3cef8a1
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,17 @@
+# 1.0.0 (2015-09-03)
+
+  * added changelog
+  * fixed RegExp escaping of `/` on node 0.10
+  * added [standard](https://github.com/feross/standard) for code style/eslint
+
+# v0.1.3 (2014-10-08)
+
+  * use toString for functions
+
+# v0.1.2 (2014-05-14)
+
+  * fixes circular reference bug
+
+# v0.1.1 (2011-04-24)
+
+  * initial release
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ccc98aa
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Marcello Bastéa-Forte (marcello at cellosoft.com)
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+    1. The origin of this software must not be misrepresented; you must not
+    claim that you wrote the original software. If you use this software
+    in a product, an acknowledgment in the product documentation would be
+    appreciated but is not required.
+
+    2. Altered source versions must be plainly marked as such, and must not be
+    misrepresented as being the original software.
+
+    3. This notice may not be removed or altered from any source
+    distribution.
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..7e62230
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,102 @@
+node-tosource
+=============
+toSource is a super simple function that converts JavaScript objects back to source code.
+
+Introduction
+------------
+Motivation: JSON doesn't support serializing functions, dates, or regular expressions. I wanted
+a quick and simple way to push trusted data structures with code from Node down to the browser.
+
+This should make it easier to share code and modules between the server and client.
+
+Installation
+------------
+
+```
+npm install tosource
+```
+
+Examples
+--------
+The following code:
+
+```js
+var toSource = require('tosource')
+console.log(toSource(
+    [ 4, 5, 6, "hello", {
+        a:2,
+        'b':3,
+        '1':4,
+        'if':5,
+        yes:true,
+        no:false,
+        nan:NaN,
+        infinity:Infinity,
+        'undefined':undefined,
+        'null':null,
+        foo: function(bar) {
+            console.log("woo! a is "+a)
+            console.log("and bar is "+bar)
+        }
+    },
+    /we$/gi,
+    new Date("Wed, 09 Aug 1995 00:00:00 GMT")]
+))
+```
+
+Output:
+
+```js
+[ 4,
+  5,
+  6,
+  "hello",
+  { "1":4,
+    a:2,
+    b:3,
+    "if":5,
+    yes:true,
+    no:false,
+    nan:NaN,
+    infinity:Infinity,
+    "undefined":undefined,
+    "null":null,
+    foo:function (bar) {
+            console.log("woo! a is "+a)
+            console.log("and bar is "+bar)
+        } },
+  /we$/gi,
+  new Date(807926400000) ]
+```
+
+
+See [test.js][1] for more examples.
+
+Supported Types
+---------------
+* Numbers
+* Strings
+* Array literals
+* object literals
+* function
+* RegExp literals
+* Dates
+* true
+* false
+* undefined
+* null
+* NaN
+* Infinity
+
+Notes
+-----
+* Functions are serialized with `func.toString()`, no closure properties are serialized
+* Multiple references to the same object become copies
+* Circular references are encoded as `{$circularReference:1}`
+
+License
+-------
+toSource is open source software under the [zlib license][2].
+
+[1]: https://github.com/marcello3d/node-tosource/blob/master/test.js
+[2]: https://github.com/marcello3d/node-tosource/blob/master/LICENSE
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..d9cab08
--- /dev/null
+++ b/package.json
@@ -0,0 +1,26 @@
+{
+  "name": "tosource",
+  "description": "toSource converts JavaScript objects back to source",
+  "version": "1.0.0",
+  "homepage": "https://github.com/marcello3d/node-tosource",
+  "repository": "git://github.com/marcello3d/node-tosource.git",
+  "author": "Marcello Bastéa-Forte <marcello at cellosoft.com> (http://marcello.cellosoft.com/)",
+  "main": "tosource.js",
+  "keywords": [
+    "source",
+    "tosource",
+    "json",
+    "javascript object",
+    "object"
+  ],
+  "dependencies": {},
+  "scripts": {
+    "test": "standard && node test.js"
+  },
+  "engines": {
+    "node": ">=0.4.0"
+  },
+  "devDependencies": {
+    "standard": "5.2.1"
+  }
+}
\ No newline at end of file
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..ec02d34
--- /dev/null
+++ b/test.js
@@ -0,0 +1,97 @@
+var toSource = require('./tosource')
+var assert = require('assert')
+
+// Various types
+var date = new Date()
+var a
+var v = toSource(
+  [ 4, 5, 6, 'hello', {
+    a: 2,
+    'b': 3,
+    '1': 4,
+    'if': 5,
+    yes: true,
+    no: false,
+    nan: NaN,
+    infinity: Infinity,
+    'undefined': undefined,
+    'null': null,
+    foo: function (bar) {
+      console.log('woo! a is ' + a)
+      console.log('and bar is ' + bar)
+    }
+  },
+    /we$/gi,
+    new RegExp('/w/e/', 'ig'),
+    /\/w\/e\//mig,
+    date,
+    new Date('Wed, 09 Aug 1995 00:00:00 GMT')]
+)
+
+assert.equal(
+  v,
+  '[ 4,\n' +
+  '  5,\n' +
+  '  6,\n' +
+  '  "hello",\n' +
+  '  { "1":4,\n' +
+  '    a:2,\n' +
+  '    b:3,\n' +
+  '    "if":5,\n' +
+  '    yes:true,\n' +
+  '    no:false,\n' +
+  '    nan:NaN,\n' +
+  '    infinity:Infinity,\n' +
+  '    "undefined":undefined,\n' +
+  '    "null":null,\n' +
+  '    foo:function (bar) {\n' +
+  '      console.log(\'woo! a is \' + a)\n' +
+  '      console.log(\'and bar is \' + bar)\n' +
+  '    } },\n' +
+  '  /we$/gi,\n' +
+  '  /\\/w\\/e\\//gi,\n' +
+  '  /\\/w\\/e\\//gim,\n' +
+  '  new Date(' + date.getTime() + '),\n' +
+  '  new Date(807926400000) ]'
+)
+
+// Filter parameter (applies to every object recursively before serializing)
+assert.equal(
+  toSource(
+    [ 4, 5, 6, { bar: 3 } ],
+    function numbersToStrings (value) {
+      return typeof value === 'number' ? '<' + value + '>' : value
+    }
+  ),
+  '[ "<4>",\n' +
+  '  "<5>",\n' +
+  '  "<6>",\n' +
+  '  { bar:"<3>" } ]'
+)
+
+// No indent
+assert.equal(
+  toSource([ 4, 5, 6, { bar: 3 } ], null, false),
+  '[4,5,6,{bar:3}]'
+)
+
+// Circular reference
+var object = {a: 1, b: 2}
+object.c = object
+
+assert.equal(
+  toSource(object),
+  '{ a:1,\n' +
+  '  b:2,\n' +
+  '  c:{$circularReference:1} }'
+)
+
+// Not a circular reference
+var foo = {}
+object = {a: foo, b: foo}
+
+assert.equal(
+  toSource(object),
+  '{ a:{},\n' +
+  '  b:{} }'
+)
diff --git a/tosource.js b/tosource.js
new file mode 100644
index 0000000..b6dbf2d
--- /dev/null
+++ b/tosource.js
@@ -0,0 +1,77 @@
+/* toSource by Marcello Bastea-Forte - zlib license */
+module.exports = function (object, filter, indent, startingIndent) {
+  var seen = []
+  return walk(object, filter, indent === undefined ? '  ' : (indent || ''), startingIndent || '', seen)
+
+  function walk (object, filter, indent, currentIndent, seen) {
+    var nextIndent = currentIndent + indent
+    object = filter ? filter(object) : object
+
+    switch (typeof object) {
+      case 'string':
+        return JSON.stringify(object)
+      case 'boolean':
+      case 'number':
+      case 'undefined':
+        return '' + object
+      case 'function':
+        return object.toString()
+    }
+
+    if (object === null) {
+      return 'null'
+    }
+    if (object instanceof RegExp) {
+      return stringifyRegExp(object)
+    }
+    if (object instanceof Date) {
+      return 'new Date(' + object.getTime() + ')'
+    }
+
+    var seenIndex = seen.indexOf(object) + 1
+    if (seenIndex > 0) {
+      return '{$circularReference:' + seenIndex + '}'
+    }
+    seen.push(object)
+
+    function join (elements) {
+      return indent.slice(1) + elements.join(',' + (indent && '\n') + nextIndent) + (indent ? ' ' : '')
+    }
+
+    if (Array.isArray(object)) {
+      return '[' + join(object.map(function (element) {
+        return walk(element, filter, indent, nextIndent, seen.slice())
+      })) + ']'
+    }
+    var keys = Object.keys(object)
+    return keys.length ? '{' + join(keys.map(function (key) {
+      return (legalKey(key) ? key : JSON.stringify(key)) + ':' + walk(object[key], filter, indent, nextIndent, seen.slice())
+    })) + '}' : '{}'
+  }
+}
+
+var KEYWORD_REGEXP = /^(abstract|boolean|break|byte|case|catch|char|class|const|continue|debugger|default|delete|do|double|else|enum|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|undefined|var|void|volatile|while|with)$/
+
+function legalKey (string) {
+  return /^[a-z_$][0-9a-z_$]*$/gi.test(string) && !KEYWORD_REGEXP.test(string)
+}
+
+// Node.js 0.10 doesn't escape slashes in re.toString() or re.source
+// when they were not escaped initially.
+// Here we check if the workaround is needed once and for all,
+// then apply it only for non-escaped slashes.
+var isRegExpEscaped = (new RegExp('/')).source === '\\/'
+
+function stringifyRegExp (re) {
+  if (isRegExpEscaped) {
+    return re.toString()
+  }
+  var source = re.source.replace(/\//g, function (found, offset, str) {
+    if (offset === 0 || str[offset - 1] !== '\\') {
+      return '\\/'
+    }
+    return '/'
+  })
+  var flags = (re.global && 'g' || '') + (re.ignoreCase && 'i' || '') + (re.multiline && 'm' || '')
+  return '/' + source + '/' + flags
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tosource.git



More information about the Pkg-javascript-commits mailing list