Skip to content

Commit e1debab

Browse files
committed
WIP nest
1 parent 8b642c2 commit e1debab

8 files changed

+238
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ nbproject
44
gitup.dat
55
.project
66
.gitup.dat
7+
_tools/nest/node_modules/

_octopress/source/_posts/2012-09-26-new-site.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ make the project ready for the future.
5050

5151
Best wishes,
5252

53-
[Kevin van Zonneveld](http://twitter.com/kvz)
53+
[Kevin](http://twitter.com/kvz)

_octopress/source/_posts/2013-05-03-a-word-on-the-focus-of-php-dot-js.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ it's cool for anybody to fork this project and run with it.
8383

8484
If anything, knowing that the php.js repository will focus on the raw `./functions`
8585
makes this easier.
86+
87+
88+
Best wishes,
89+
90+
[Kevin](http://twitter.com/kvz)
91+

_tools/_temp/empty

Whitespace-only changes.

_tools/nest/nest.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var Q = require('q');
2+
var FS = require('fs');
3+
var cli = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
4+
5+
cli.parse({
6+
function_file: ['f', 'Function to test', 'path'],
7+
});
8+
9+
10+
function Phpjs () {
11+
}
12+
13+
Phpjs.prototype._read = function(file, cb) {
14+
FS.readFile(file, 'utf-8', function (error, code) {
15+
if (error) {
16+
return cb(error);
17+
}
18+
19+
cb(null, code);
20+
});
21+
};
22+
23+
Phpjs.prototype._commentBlocks = function(code) {
24+
var cnt = 0;
25+
var comment = [];
26+
var commentBlocks = [];
27+
var i = 0;
28+
var lines = [];
29+
var raise = false;
30+
for (i in (lines = code.replace('\r', '').split('\n'))) {
31+
// Detect if line is a comment, and return the actual comment
32+
if ((comment = lines[i].match(/^\s*(\/\/|\/\*|\*)\s*(.*)$/))) {
33+
if (raise === true) {
34+
cnt++;
35+
raise = false;
36+
}
37+
if (!commentBlocks[cnt]) {
38+
commentBlocks[cnt] = {clean: [], raw: [], };
39+
}
40+
41+
commentBlocks[cnt].clean.push(comment[2].trim());
42+
commentBlocks[cnt].raw.push(lines[i]);
43+
} else {
44+
raise = true;
45+
}
46+
}
47+
48+
return commentBlocks;
49+
}
50+
51+
Phpjs.prototype.parse = function(code, cb) {
52+
var commentBlocks = this._commentBlocks(code);
53+
var head = commentBlocks[0];
54+
// var body = code.replace(head, '');
55+
56+
// return cb(null, commentBlocks[1]);
57+
58+
cb(null, {
59+
// code: code,
60+
// body: body,
61+
head: head,
62+
commentBlocks: commentBlocks,
63+
});
64+
};
65+
66+
Phpjs.prototype.load = function(file, cb) {
67+
var self = this;
68+
self._read(file, function (err, code) {
69+
if (err) {
70+
return cb(err);
71+
}
72+
73+
self.parse(code, function (err, result) {
74+
if (err) {
75+
return cb(err);
76+
}
77+
78+
return cb(null, result);
79+
});
80+
});
81+
};
82+
83+
84+
85+
86+
cli.main(function(args, options) {
87+
if (!options.function_file) {
88+
this.fatal('Please specify a file to test (-h for help)');
89+
}
90+
this.ok('Going to test: ' + options.function_file);
91+
// cli.spinner('Working..');
92+
// cli.spinner('Working.. done!', true); //End the spinner
93+
94+
var phpjs = new Phpjs();
95+
96+
phpjs.load(options.function_file, function (err, result) {
97+
if (err) {
98+
return cli.fatal(err);
99+
}
100+
console.log(result);
101+
// cli.ok('result' + result);
102+
});
103+
104+
105+
// setTimeout(function () {
106+
// cli.spinner('Working.. done!', true); //End the spinner
107+
// }, 3000);
108+
109+
});

_tools/nest/optimized.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
function strtotime(text, now) {
2+
if(!text) {
3+
return null
4+
}
5+
text = text.trim().replace(/\s{2,}/g, " ").replace(/[\t\r\n]/g, "").toLowerCase();
6+
var parsed;
7+
if(text === "now") {
8+
return now === null || isNaN(now) ? (new Date).getTime() / 1E3 | 0 : now | 0
9+
}else {
10+
if(!isNaN(parse = Date.parse(text))) {
11+
return parse / 1E3 | 0
12+
}
13+
}
14+
if(text === "now") {
15+
return(new Date).getTime() / 1E3
16+
}else {
17+
if(!isNaN(parsed = Date.parse(text))) {
18+
return parsed / 1E3
19+
}
20+
}
21+
var match = text.match(/^(\d{2,4})-(\d{2})-(\d{2})(?:\s(\d{1,2}):(\d{2})(?::\d{2})?)?(?:\.(\d+)?)?$/);
22+
if(match) {
23+
var year = match[1] >= 0 && match[1] <= 69 ? +match[1] + 2E3 : match[1];
24+
return new Date(year, parseInt(match[2], 10) - 1, match[3], match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1E3
25+
}
26+
var date = now ? new Date(now * 1E3) : new Date;
27+
var days = {"sun":0, "mon":1, "tue":2, "wed":3, "thu":4, "fri":5, "sat":6};
28+
var ranges = {"yea":"FullYear", "mon":"Month", "day":"Date", "hou":"Hours", "min":"Minutes", "sec":"Seconds"};
29+
function lastNext(type, range, modifier) {
30+
var day = days[range];
31+
if(typeof day !== "undefined") {
32+
var diff = day - date.getDay();
33+
if(diff === 0) {
34+
diff = 7 * modifier
35+
}else {
36+
if(diff > 0 && type === "last") {
37+
diff -= 7
38+
}else {
39+
if(diff < 0 && type === "next") {
40+
diff += 7
41+
}
42+
}
43+
}
44+
date.setDate(date.getDate() + diff)
45+
}
46+
}
47+
function process(val) {
48+
var split = val.split(" ");
49+
var type = split[0];
50+
var range = split[1].substring(0, 3);
51+
var typeIsNumber = /\d+/.test(type);
52+
var ago = split[2] === "ago";
53+
var num = (type === "last" ? -1 : 1) * (ago ? -1 : 1);
54+
if(typeIsNumber) {
55+
num *= parseInt(type, 10)
56+
}
57+
if(ranges.hasOwnProperty(range)) {
58+
return date["set" + ranges[range]](date["get" + ranges[range]]() + num)
59+
}else {
60+
if(range === "wee") {
61+
return date.setDate(date.getDate() + num * 7)
62+
}
63+
}
64+
if(type === "next" || type === "last") {
65+
lastNext(type, range, num)
66+
}else {
67+
if(!typeIsNumber) {
68+
return false
69+
}
70+
}
71+
return true
72+
}
73+
var regex = "([+-]?\\d+\\s" + "(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?" + "|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday" + "|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)|(last|next)\\s" + "(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?" + "|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday" + "|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))(\\sago)?";
74+
match = text.match(new RegExp(regex, "gi"));
75+
if(!match) {
76+
return false
77+
}
78+
for(var i = 0, len = match.length;i < len;i++) {
79+
if(!process(match[i])) {
80+
return false
81+
}
82+
}
83+
return date.getTime() / 1E3
84+
}
85+
;

_tools/nest/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "nest",
3+
"version": "0.0.1",
4+
"description": "php.js node testing",
5+
"homepage": "http://phpjs.org",
6+
"author": "Kevin van Zonneveld <kevin@vanzonneveld.net>",
7+
"engines": {
8+
"node": ">= 0.6.0"
9+
},
10+
"dependencies": {
11+
"q": "~0.9.3"
12+
},
13+
"keywords": [
14+
"cli",
15+
"console",
16+
"test",
17+
"phpjs"
18+
],
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/kvz/phpjs.git"
22+
},
23+
"scripts": {
24+
"test": "make test"
25+
}
26+
}

_tools/whitespace_closure.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SOURCE="../../functions/datetime/strtotime.js"
2+
curl -o optimized.js \
3+
-X POST \
4+
-H 'Expect: ' \
5+
--data-urlencode compilation_level="WHITESPACE_ONLY" \
6+
--data-urlencode output_format="text" \
7+
--data-urlencode output_info="compiled_code" \
8+
--data-urlencode formatting=pretty_print \
9+
--data-urlencode js_code@${SOURCE} \
10+
http://closure-compiler.appspot.com/compile

0 commit comments

Comments
 (0)