Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: kvz/phpjs
...
head fork: kvz/phpjs
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 19, 2012
@Ghabriel Ghabriel I created this new explode function, which fixes several bugs that I …
…found in the previous version. Note: sorry for any english mistakes, english is not my first language. I also posted this function in http://phpjs.org/functions/explode:396#comment_153595
04d0c07
Commits on Mar 20, 2012
@kvz Merge pull request #30 from Ghabriel/patch-1
I created this new explode function, which fixes several bugs that I found...
053c242
Showing with 29 additions and 38 deletions.
  1. +29 −38 functions/strings/explode.js
View
67 functions/strings/explode.js
@@ -1,42 +1,33 @@
function explode (delimiter, string, limit) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: kenneth
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: d3x
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // * example 1: explode(' ', 'Kevin van Zonneveld');
- // * returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
- // * example 2: explode('=', 'a=bc=d', 2);
- // * returns 2: ['a', 'bc=d']
- var emptyArray = {
- 0: ''
- };
- // third argument is not required
- if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {
- return null;
- }
+ if ( arguments.length < 2 || typeof delimiter == 'undefined' || typeof string == 'undefined' ) return null;
+ if ( delimiter === '' || delimiter === false || delimiter === null) return false;
+ if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object'){
+ return { 0: '' };
+ }
+ if ( delimiter === true ) delimiter = '1';
+
+ // Here we go...
+ delimiter += '';
+ string += '';
+
+ var s = string.split( delimiter );
+
- if (delimiter === '' || delimiter === false || delimiter === null) {
- return false;
- }
+ if ( typeof limit === 'undefined' ) return s;
+
+ // Support for limit
+ if ( limit === 0 ) limit = 1;
+
+ // Positive limit
+ if ( limit > 0 ){
+ if ( limit >= s.length ) return s;
+ return s.slice( 0, limit - 1 ).concat( [ s.slice( limit - 1 ).join( delimiter ) ] );
+ }
- if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
- return emptyArray;
- }
-
- if (delimiter === true) {
- delimiter = '1';
- }
-
- if (!limit) {
- return string.toString().split(delimiter.toString());
- }
- // support for limit argument
- var splitted = string.toString().split(delimiter.toString());
- var partA = splitted.splice(0, limit - 1);
- var partB = splitted.join(delimiter.toString());
- partA.push(partB);
- return partA;
-}
+ // Negative limit
+ if ( -limit >= s.length ) return [];
+
+ s.splice( s.length + limit );
+ return s;
+}

No commit comments for this range

Something went wrong with that request. Please try again.