Skip to content

Commit 24a16b6

Browse files
committed
Always include ini_set and ini_get for now
1 parent 111a290 commit 24a16b6

File tree

6 files changed

+119
-69
lines changed

6 files changed

+119
-69
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ setup:
55
cd .. ; \
66

77
test:
8-
cd _tests && npm install
9-
find functions -type f |grep -v '/_' |xargs node _tests/cli.js -f
8+
cd tests && npm install
9+
find functions -type f |grep -v '/_' |awk -F/ '{print $$NF}' |sed 's@\.js$$@@g' |xargs node tests/cli.js -f
1010

1111
site:
1212
git pull && \

functions/array/array_change_key_case.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function array_change_key_case (array, cs) {
22
// From: http://phpjs.org/functions
33
// + original by: Ates Goral (http://magnetiq.com)
44
// + improved by: marrtins
5-
// + improved by: Brett Zamir (http://brett-zamir.me)
5+
// + improved by: Brett Zamir (http://brett-zamir.me)
66
// * example 1: array_change_key_case(42);
77
// * returns 1: false
88
// * example 2: array_change_key_case([ 3, 5 ]);
@@ -19,7 +19,7 @@ function array_change_key_case (array, cs) {
1919
// * example 7: var arr = array({a: 0}, {B: 1}, {c: 2});
2020
// * example 7: var newArr = array_change_key_case(arr);
2121
// * example 7: newArr.b
22-
// * example 7: 1
22+
// * returns 7: 1
2323

2424
var case_fn, key, tmp_ar = {};
2525

@@ -36,5 +36,6 @@ function array_change_key_case (array, cs) {
3636
}
3737
return tmp_ar;
3838
}
39+
3940
return false;
4041
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
function ini_get (varname) {
2-
// http://kevin.vanzonneveld.net
2+
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// % note 1: The ini values must be set by ini_set or manually within an ini file
55
// * example 1: ini_get('date.timezone');
66
// * returns 1: 'Asia/Hong_Kong'
7+
78
if (this.php_js && this.php_js.ini && this.php_js.ini[varname] && this.php_js.ini[varname].local_value !== undefined) {
89
if (this.php_js.ini[varname].local_value === null) {
910
return '';
1011
}
1112
return this.php_js.ini[varname].local_value;
1213
}
14+
1315
return '';
1416
}

experimental/info/ini_set.js renamed to functions/info/ini_set.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
function ini_set (varname, newvalue) {
2-
// http://kevin.vanzonneveld.net
2+
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// % note 1: This will not set a global_value or access level for the ini item
55
// * example 1: ini_set('date.timezone', 'America/Chicago');
66
// * returns 1: 'Asia/Hong_Kong'
77

8-
var oldval = '',
9-
that = this;
10-
this.php_js = this.php_js || {};
11-
this.php_js.ini = this.php_js.ini || {};
8+
var oldval = '';
9+
var self = this;
10+
this.php_js = this.php_js || {};
11+
this.php_js.ini = this.php_js.ini || {};
1212
this.php_js.ini[varname] = this.php_js.ini[varname] || {};
13-
oldval = this.php_js.ini[varname].local_value;
13+
oldval = this.php_js.ini[varname].local_value;
1414

15-
var _setArr = function (oldval) { // Although these are set individually, they are all accumulated
15+
var _setArr = function (oldval) {
16+
// Although these are set individually, they are all accumulated
1617
if (typeof oldval === 'undefined') {
17-
that.php_js.ini[varname].local_value = [];
18+
self.php_js.ini[varname].local_value = [];
1819
}
19-
that.php_js.ini[varname].local_value.push(newvalue);
20+
self.php_js.ini[varname].local_value.push(newvalue);
2021
};
2122

2223
switch (varname) {
2324
case 'extension':
2425
if (typeof this.dl === 'function') {
25-
this.dl(newvalue); // This function is only experimental in php.js
26+
// This function is only experimental in php.js
27+
this.dl(newvalue);
2628
}
2729
_setArr(oldval, newvalue);
2830
break;

tests/cli.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
var cli = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
2-
var FS = require('fs');
3-
var glob = require('glob');
4-
var PhpjsUtil = require('./phpjsutil');
1+
var cli = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
2+
var FS = require('fs');
3+
var glob = require('glob');
4+
var phpjsutil = new require('./phpjsutil');
5+
6+
var PhpjsUtil = phpjsutil({
7+
injectDependencies: [ 'ini_set', 'ini_get' ]
8+
});
59

610
// Environment-specific file opener. function name needs to
711
// be translated to code. The difficulty is in finding the
@@ -61,7 +65,7 @@ cli.main(function(args, options) {
6165
return cli.fatal(err);
6266
}
6367

64-
console.log(params['headKeys']);
68+
// console.log(params['headKeys']);
6569

6670
PhpjsUtil.test(params, function(err, test, params) {
6771
var testline = cli.pad(params['name'] + '#' + test['number'], (width * 0.4), ' ', 'right') +
@@ -72,6 +76,7 @@ cli.main(function(args, options) {
7276

7377
if (err) {
7478
cli.error(testline + '');
79+
cli.error(err);
7580
} else {
7681
cli.ok(' ' + testline + '');
7782
}

tests/phpjsutil.js

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
if (typeof require !== 'undefined') {
2-
module.exports = PhpjsUtil;
2+
module.exports = function phpjsutil(config) {
3+
var self = new PhpjsUtil(config);
4+
return self;
5+
};
36
}
4-
function PhpjsUtil () {
7+
8+
function PhpjsUtil(config) {
9+
for (var k in config) {
10+
this[k] = config[k];
11+
}
512
}
613

7-
PhpjsUtil._commentBlocks = function(code) {
8-
var cnt = 0;
9-
var comment = [];
14+
PhpjsUtil.prototype._commentBlocks = function(code) {
15+
var cnt = 0;
16+
var comment = [];
1017
var commentBlocks = [];
11-
var i = 0;
12-
var lines = [];
13-
var raise = false;
18+
var i = 0;
19+
var lines = [];
20+
var raise = false;
1421
for (i in (lines = code.replace('\r', '').split('\n'))) {
1522
// Detect if line is a comment, and return the actual comment
1623
if ((comment = lines[i].match(/^\s*(\/\/|\/\*|\*)\s*(.*)$/))) {
1724
if (raise === true) {
18-
cnt = commentBlocks.length;
25+
cnt = commentBlocks.length;
1926
raise = false;
2027
}
2128
if (!commentBlocks[cnt]) {
@@ -32,14 +39,15 @@ PhpjsUtil._commentBlocks = function(code) {
3239
return commentBlocks;
3340
};
3441

35-
PhpjsUtil._headKeys = function(headLines) {
42+
PhpjsUtil.prototype._headKeys = function(headLines) {
3643
var i;
37-
var keys = {};
38-
var match = [];
44+
var keys = {};
45+
var match = [];
3946
var dmatch = [];
40-
var key = '';
41-
var val = '';
42-
var num = 0;
47+
var key = '';
48+
var val = '';
49+
var num = 0;
50+
4351
for (i in headLines) {
4452
if (!(match = headLines[i].match(/^[\s\W]*(.*?)\s*:\s*(.*)\s*$/))) {
4553
continue;
@@ -70,70 +78,92 @@ PhpjsUtil._headKeys = function(headLines) {
7078
return keys;
7179
};
7280

73-
PhpjsUtil._loadDependencies = function(headKeys, dependencies, cb) {
74-
var self = this;
81+
PhpjsUtil.prototype.contains = function (array, value) {
82+
var i = array.length;
83+
while (i--) {
84+
if (array[i] === value) {
85+
return true;
86+
}
87+
}
88+
return false;
89+
};
90+
91+
PhpjsUtil.prototype._loadDependencies = function(name, headKeys, dependencies, cb) {
92+
var self = this;
93+
var functions = headKeys['depends on'] || [];
94+
95+
var j;
96+
for (j in self.injectDependencies) {
97+
var injectFunction = self.injectDependencies[j];
98+
if (self.contains(self.injectDependencies, name)) {
99+
continue;
100+
}
101+
102+
// console.log({injectFunction: injectFunction});
103+
functions.push(injectFunction);
104+
}
75105

76-
if (!headKeys['depends on'] || !headKeys['depends on'].length) {
106+
if (!functions || !functions.length) {
77107
if (cb) {
78108
cb(null, {});
79109
}
80110
}
81111

112+
// console.log({functions: functions, headKeys: headKeys, dependencies: dependencies});
82113
var i;
83114
var name;
84115
var loaded = 0;
85-
for (i in headKeys['depends on']) {
86-
name = headKeys['depends on'][i];
116+
for (i in functions) {
117+
name = functions[i];
87118

88119
self.load(name, function (err, params) {
89120
if (err) {
90121
return cb(err);
91122
}
92123

93124
dependencies[name] = params;
94-
self._loadDependencies(params.headKeys, dependencies);
125+
self._loadDependencies(name, params.headKeys, dependencies);
95126

96127
loaded++;
97-
98-
if (cb && loaded === headKeys['depends on'].length) {
128+
if (cb && loaded === functions.length) {
99129
cb(null, dependencies);
100130
}
101131
});
102132
}
103133
};
104134

105-
PhpjsUtil.parse = function(name, code, cb) {
135+
PhpjsUtil.prototype.parse = function(name, code, cb) {
106136
var commentBlocks = this._commentBlocks(code);
107-
var head = commentBlocks[0].raw.join('\n');
108-
var body = code.replace(head, '');
109-
110-
var headKeys = this._headKeys(commentBlocks[0].clean);
137+
var head = commentBlocks[0].raw.join('\n');
138+
var body = code.replace(head, '');
139+
var headKeys = this._headKeys(commentBlocks[0].clean);
111140

112141
// @todo(kvz) If we add function signature, we can use
113142
// body to generate CommonJs compatible output
114143
// in the browser.
115144

116-
this._loadDependencies(headKeys, {}, function (err, dependencies) {
145+
this._loadDependencies(name, headKeys, {}, function (err, dependencies) {
117146
if (err) {
118147
return cb(err);
119148
}
149+
120150
cb(null, {
121-
headKeys: headKeys,
122-
body: body,
123-
head: head,
124-
name: name,
125-
code: code,
126-
dependencies: dependencies,
151+
headKeys : headKeys,
152+
body : body,
153+
head : head,
154+
name : name,
155+
code : code,
156+
dependencies : dependencies,
127157
commentBlocks: commentBlocks
128158
});
129159
});
130160
};
131161

132-
PhpjsUtil.opener = function(name, cb) {
162+
PhpjsUtil.prototype.opener = function(name, cb) {
133163
return cb('Please override with a method that can translate a function-name to code in your environment');
134164
};
135165

136-
PhpjsUtil.load = function(name, cb) {
166+
PhpjsUtil.prototype.load = function(name, cb) {
137167
var self = this;
138168
self.opener(name, function (err, code) {
139169
if (err) {
@@ -150,10 +180,10 @@ PhpjsUtil.load = function(name, cb) {
150180
});
151181
};
152182

153-
PhpjsUtil.test = function(params, cb) {
154-
var i = 0;
155-
var j = 0;
156-
var d = 0;
183+
PhpjsUtil.prototype.test = function(params, cb) {
184+
var i = 0;
185+
var j = 0;
186+
var d = 0;
157187
var self = this;
158188

159189

@@ -164,11 +194,12 @@ PhpjsUtil.test = function(params, cb) {
164194
for (d in params['dependencies']) {
165195
codes.push(params['dependencies'][d]['code']);
166196
}
197+
console.log({codes: codes});
167198

168199
// Reverse stack so dependencies are loaded first
169200
codes = codes.reverse();
170201

171-
// console.log(codes);
202+
// console.log({params: params});
172203

173204
// Load code
174205
eval(codes.join('\n'));
@@ -178,9 +209,14 @@ PhpjsUtil.test = function(params, cb) {
178209
for (i in params['headKeys']['example']) {
179210
var test = {
180211
example: params['headKeys']['example'][i].join('\n'),
181-
number: i
212+
number : i
182213
};
183214

215+
if (!params['headKeys']['returns'][i] || !params['headKeys']['returns'][i].length) {
216+
cb('There is no return for example ' + i, test, params);
217+
continue;
218+
}
219+
184220
// Needs an eval so types are cast properly, also, expected may
185221
// contain code
186222
eval('test.expected = ' + params['headKeys']['returns'][i].join('\n') + '');
@@ -190,13 +226,17 @@ PhpjsUtil.test = function(params, cb) {
190226
eval('test.result = ' + params['headKeys']['example'][i][j] + '');
191227
}
192228

193-
if (test.expected !== test.result) {
194-
var err = 'Expected: ' + JSON.stringify(test.expected, undefined, 2) +
195-
' but returned: ' + JSON.stringify(test.result, undefined, 2);
229+
var jsonExpected = JSON.stringify(test.expected, undefined, 2);
230+
var jsonResult = JSON.stringify(test.result, undefined, 2);
231+
232+
if (jsonExpected !== jsonResult) {
233+
var err = 'Expected: ' + jsonExpected +
234+
' but returned: ' + jsonResult;
196235
cb(err, test, params);
197-
} else {
198-
cb(null, test, params);
236+
continue;
199237
}
238+
239+
cb(null, test, params);
240+
continue;
200241
}
201242
};
202-

0 commit comments

Comments
 (0)