Skip to content

Instantly share code, notes, and snippets.

@bxt
Created October 12, 2011 20:46
Show Gist options
  • Save bxt/1282490 to your computer and use it in GitHub Desktop.
Save bxt/1282490 to your computer and use it in GitHub Desktop.
Javascript batch processing filename utilities
/**
* Return something as filename-save string.
*
* Result will contain only 0-9, a-z and "-".
* It converts german umlauts, you might want
* to add some conversions for your target
* language.
*/
function filesc(str) {
return (str+'')
.toLowerCase()
.replace('ö','oe')
.replace('ä','ae')
.replace('ü','ue')
.replace('ß','ss')
.replace(/[^0-9A-Za-z]+/g,'-')
.replace(/^-|-$/g,'')
;
}
/**
* Pad a number n with c (default:5) zeros.
*
* For input n=42 returns "00042".
*/
function zeropad(n,c) {
n=n*1;
c=c||5;
var digits = n==0 ? 1 : Math.floor(Math.log(n)/Math.log(10))+1 ;
return new Array(c-digits+1).join('0')+n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment