Skip to content

Commit

Permalink
Monday exercises 15/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Giordo committed Feb 15, 2016
1 parent c6cda3d commit 2062c96
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Js/get_time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function convert(time) {
var hours = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(),
minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(),
seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds(),
milliseconds = time.getMilliseconds() < 100 ? (time.getMilliseconds() < 10 ? '00' + time.getMilliseconds() : '0' + time.getMilliseconds()) : time.getMilliseconds();

return [hours, minutes, seconds].join(':') + ',' + milliseconds;
}
26 changes: 26 additions & 0 deletions Js/remove_repeating_commas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function dadFilter(str){
return str.replace(/^[, ]+|[, ]+$/g, "").replace(/,+/g, ",");
}


// or

function dadFilter(str){
return str.replace(/,{2,}/g,",").replace(/,+( +)?$/,"");
}

function dadFilter(str) {
return str.replace(/,,+/g, ",").replace(/,\s*$/, "")
}

function dadFilter(str){
return str.replace(/,+/g, ",").replace(/(,|\s)+$/, "");
}

function dadFilter(str){
str = str.replace(/,+/g, ",")
.trim()
.replace(/,$/, "");

return str;
}
55 changes: 55 additions & 0 deletions Js/snake-dash to camelcase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function toCamelCase(str){
return str.replace(/(_|-)([a-z])/gi, toUpperCase );

}
function toUpperCase(str){
console.log(str[1]);
return str[1].toUpperCase();
}

// or

function toCamelCase(str){
var regExp=/[-_]\w/ig;
return str.replace(regExp,function(match){
return match.charAt(1).toUpperCase();
});
}

// or

function toCamelCase(str) {
return str.replace(/[_-][A-Za-z]/g, function(match) {return match[1].toUpperCase();});
}

// complex

function toCamelCase(str){
var demarcs ='-_';
return str.split('').map(function(value,index,array){
if(demarcs.indexOf(value) > -1){
return '';
} else if (demarcs.indexOf(array[index - 1]) > -1){
return value.toUpperCase();
} else {
return value;
}
}).join('');
}

// or

unction toCamelCase(str){
var strArray;
if (str.indexOf('-') !== -1){ //if delineated by -
strArray = str.split('-');
} else {
strArray = str.split('_'); //if delineated by _
}
var camelCase = strArray[0]; //keeps first word value as is
for (var i=1, len=strArray.length; i < len; i++){
var capitalized = strArray[i].substr(0, 1).toUpperCase() + strArray[i].slice(1);
camelCase += capitalized;
}
return camelCase;
}
21 changes: 21 additions & 0 deletions Py/dash-snake to camelcase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import re
def to_camel_case(str):
return re.sub(r'(_|-)([a-zA-Z])', upper_repl , str)

def upper_repl(match):
return "" + match.group(2).upper()

# no regex

def to_camel_case(text):
words = text.replace('_', '-').split('-')
return words[0] + ''.join([x.title() for x in words[1:]])

# other regex

from re import compile as reCompile

PATTERN = reCompile(r'(?i)[-_]([a-z])')

def to_camel_case(text):
return PATTERN.sub(lambda m: m.group(1).upper(), text)

0 comments on commit 2062c96

Please sign in to comment.