Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tunnckoCore/8a8aab0a9291f452318b to your computer and use it in GitHub Desktop.
Save tunnckoCore/8a8aab0a9291f452318b to your computer and use it in GitHub Desktop.
Custom split function. A tweet-sized, fork-to-play, custom split function implementation, lol. But it is **very very** slow!

custom split

A tweet-sized, fork-to-play, custom split function implementation, lol. But it is very very slow!

Usage

var path = '/home/charlike/dev/kdf/templates/index.js';
var _split = function(a,b){var c=[],d='',i=0,j=0,f;while(i<a.length){f=a[i++];if(f===b){j++;d=''}else{d=d+f;c[j]=d}}if(!c[0]){c[0]=''}return c}
_split(path, '/');
//=> ['', 'home', 'charlike', 'dev', 'kdf', 'templates', 'index.js']
function(
a, /* incoming string that will split */
b /* based on some character */
) {
var c = [], // array that we will return finally
d = '', // used for caching
i = 0, // character iterator
j = 0, // array key
f; // current character
while (i < a.length) { // loop over all chars in string
f = a[i++]; // get current char
if (f === b) { // check if it is equal to the given `b`
j++; // we should increase array key
d = '' // and we should reset string cache
} else { // otherwise
d = d + f; // we concatenate current char with previous char
c[j] = d // and write current state to as array value
}
}
if (!c[0]) { // weird, but we should check if first value
c[0] = '' // of the constructed array is undefined
} // if it is, reset it to null string
return c // return array that we built
}
function(a,b){var c=[],d='',i=0,j=0,f;while(i<a.length){f=a[i++];if(f===b){j++;d=''}else{d=d+f;c[j]=d}}if(!c[0]){c[0]=''}return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011-2015 Charlike Mike Reagent <http://j.mp/1stW47C>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "customSplitFunction",
"description": "Very custom and very slow implementation of split function",
"keywords": [
"string",
"split",
"regex",
"custom",
"slow"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>,home,charlike,dev,kdf,templates,index.js</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var path = '/home/charlike/dev/kdf/templates/index.js';
var _split = function(a,b){var c=[],d='',i=0,j=0,f;while(i<a.length){f=a[i++];if(f===b){j++;d=''}else{d=d+f;c[j]=d}}if(!c[0]){c[0]=''}return c}
var myFunction = function(){return _split(path, '/')}
document.getElementById( "ret" ).innerHTML = myFunction()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment