Skip to content

Commit

Permalink
Add interoperability hack to npm for functions that were built for br…
Browse files Browse the repository at this point in the history
…owsers. Fixes #145
  • Loading branch information
kvz committed Mar 4, 2014
1 parent 7226b64 commit ff46726
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
19 changes: 18 additions & 1 deletion bin/phpjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ cli.parse({
var PhpjsUtil = phpjsutil({
injectDependencies: ['ini_set', 'ini_get'],
equal : equal,
debug : cli.debug
debug : cli.debug,
globals : {
'XMLHttpRequest': '{}',
'window': '{' +
'document: {' +
'lastModified: 1388954399,' +
'getElementsByTagName: function(){return [];}' +
'},' +
'location: {' +
'href: ""' +
'}' +
'}',
}
});

// Environment-specific file opener. function name needs to
Expand Down Expand Up @@ -152,6 +164,11 @@ cli.buildnpm = function(args, options) {
fs.appendFileSync(options.output, '// \n');
fs.appendFileSync(options.output, '// Make function changes in ./functions and \n');
fs.appendFileSync(options.output, '// generator changes in ./lib/phpjsutil.js \n');

for (var global in PhpjsUtil.globals) {
fs.appendFileSync(options.output, 'exports.' + global + ' = ' + PhpjsUtil.globals[global] + ';\n');
}

self.glob(pattern, function (err, params, file) {
if (err) {
return self.error('Could not glob for ' + pattern + '. ' + err);
Expand Down
52 changes: 29 additions & 23 deletions build/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// Make function changes in ./functions and
// generator changes in ./lib/phpjsutil.js
exports.XMLHttpRequest = {};
exports.window = {document: {lastModified: 1388954399,getElementsByTagName: function(){return [];}},location: {href: ""}};

exports.array = function () {
try {
Expand Down Expand Up @@ -1229,23 +1231,6 @@ exports.array_pop = function (inputArr) {
}
};

exports.array_product = function (input) {
var idx = 0,
product = 1,
il = 0;

if (Object.prototype.toString.call(input) !== '[object Array]') {
return null;
}

il = input.length;
while (idx < il) {
product *= (!isNaN(input[idx]) ? input[idx] : 0);
idx++;
}
return product;
};

exports.array_push = function (inputArr) {
var i = 0,
pr = '',
Expand Down Expand Up @@ -1278,6 +1263,23 @@ exports.array_push = function (inputArr) {
return len + i - 1;
};

exports.array_product = function (input) {
var idx = 0,
product = 1,
il = 0;

if (Object.prototype.toString.call(input) !== '[object Array]') {
return null;
}

il = input.length;
while (idx < il) {
product *= (!isNaN(input[idx]) ? input[idx] : 0);
idx++;
}
return product;
};

exports.array_rand = function (input, num_req) {
var indexes = [];
var ticks = num_req || 1;
Expand Down Expand Up @@ -8242,11 +8244,6 @@ exports.is_object = function (mixed_var) {
return mixed_var !== null && typeof mixed_var === 'object';
};

exports.is_scalar = function (mixed_var) {
return (/boolean|number|string/)
.test(typeof mixed_var);
};

exports.is_resource = function (handle) {
var getFuncName = function(fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/)
Expand All @@ -8260,6 +8257,11 @@ exports.is_resource = function (handle) {
'PHPJS_Resource');
};

exports.is_scalar = function (mixed_var) {
return (/boolean|number|string/)
.test(typeof mixed_var);
};

exports.is_string = function (mixed_var) {
return (typeof mixed_var === 'string');
};
Expand Down Expand Up @@ -10526,7 +10528,11 @@ exports.setlocale = function (category, locale) {
};
// END STATIC
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
try {
this.php_js = this.php_js || {};
} catch (e) {
this.php_js = {};
}

var phpjs = this.php_js;

Expand Down
8 changes: 6 additions & 2 deletions functions/strings/setlocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ function setlocale(category, locale) {
};
// END STATIC
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
try {
this.php_js = this.php_js || {};
} catch (e) {
this.php_js = {};
}

This comment has been minimized.

Copy link
@brettz9

brettz9 Mar 5, 2014

Collaborator

I don't see how this block will help anything...Why would there be a problem getting an undefined property?

This comment has been minimized.

Copy link
@kvz

kvz Mar 5, 2014

Author Collaborator

This comment has been minimized.

Copy link
@brettz9

brettz9 Mar 5, 2014

Collaborator

Ok...I'm just surprised this isn't causing more problems elsewhere as this pattern is frequently in use... Oh well, if it works, it works...


var phpjs = this.php_js;

Expand Down Expand Up @@ -343,4 +347,4 @@ function setlocale(category, locale) {
this.php_js.localeCategories[category] = locale;
}
return locale;
}
}
4 changes: 3 additions & 1 deletion functions/strings/str_word_count.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function str_word_count(str, format, charlist) {
// returns 2: {0: 'Hello', 6: 'fri', 10: 'nd', 14: "you're", 29: 'looking', 46: 'good', 51: 'today'}
// example 3: str_word_count("Hello fri3nd, you're\r\n looking good today!", 1, '\u00e0\u00e1\u00e3\u00e73');
// returns 3: ['Hello', 'fri3nd', "you're", 'looking', 'good', 'today']
// example 4: str_word_count('hey', 2);
// returns 4: {0: 'hey'}

var len = str.length,
cl = charlist && charlist.length,
Expand Down Expand Up @@ -102,4 +104,4 @@ function str_word_count(str, format, charlist) {
}

throw 'You have supplied an incorrect format';
}
}
15 changes: 3 additions & 12 deletions lib/phpjsutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,9 @@ PhpjsUtil.prototype.test = function(params, cb) {
var extracted = self.getRecursiveCode(params);
codez = codez.concat(codez, extracted);

codez.unshift('' +
'XMLHttpRequest = {};' +
'window = {' +
'document: {' +
'lastModified: 1388954399,' +
'getElementsByTagName: function(){return [];}' +
'},' +
'location: {' +
'href: ""' +
'}' +
'};' +
'');
for (var global in self.globals) {
codez.unshift(global + ' = ' + self.globals[global] + ';');
}

// Load code
// self. refers to own function
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phpjs",
"version": "1.0.2",
"version": "1.1.2",
"description": "php.js offers community built php functions in javascript",
"homepage": "http://phpjs.org",
"author": "Kevin van Zonneveld <kevin@vanzonneveld.net>",
Expand Down

0 comments on commit ff46726

Please sign in to comment.