Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

monkeymonk/jsporn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

jsporn

Strings

Making text L337

function L337(str) { 
 return str.replace(/[a-z]/g, function f(a) {
 return "4BCD3F6H1JKLMN0PQR57"[parseInt(a, 36) - 10] || a.replace(/[a-t]/gi, f);
 }); 
}

Author: Markandey Singh / Source: JSOneLiners

Padding numbers

function pad(num, length) {
 return (Array(length).join('0') + num).slice(-length);
}
pad(6, 2) // 06
pad(13, 10) // 0000000013

Conversion

ParseInt

console.log(["10", "10", "10"].map(parseInt));
// return [10, NaN, 2]

Random

Incrementing stuff with nested loops

Because why not ?

for (var i = 0, j = 0; i < 10 && j < 10; j++, i = (j == 10) ? i + 1 : i, j = (j == 10) ? j = 0 : j, console.log(i, j)) {}

Author: Nicholas Ortenzio / Source: Medium

Getting URL parameters

function getQueryParams() {
 return document.location.search.replace(/(^\?)/, '').split('&').reduce(function (o, n) {
 n = n.split('='); o[n[0]] = n[1]; return o;
 }, {});
}
getQueryParams() // {key: "value"}

Author: Nicholas Ortenzio / Source: Medium

Debugging CSS

[].forEach.call($$("*"), function (a) {
 a.style.outline = "1px solid #" + (~~(Math.random() * (1 << 24))).toString(16);
});

Author: Addy Osmani / Source: arqex

Array concat unique

var newArray = [].concat(a, b.filter(function (item) {
 return a.indexOf(item) < 0;
}));

Array randow

[1, 2, 3, 4, 5].sort(function () { return Math.random() - 0.5; });

Array find smallest/largest

var larget = Math.max.apply(Math, [0, 5, 19, 7, -1]); // 19
var smallest = Math.min.apply(Math, [0, 5, 19, 7, -1]); // -1

Array create + populate

Array.apply(null, {length: 10}).map(Number.call, Number); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Object indexOf

var index = [{id: 3}].map(function (e) { return e.id; }).indexOf();

URI Parser

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"

"Y-m-d H:m:s" to JS Date

function convertDate(time) {
 var CustomDate = Function.prototype.bind.apply(Date, [null].concat(time.split(/[\s:-]/)).map(function (v, i) {
 return (i === 2 ? --v : v); 
 }));
 return new CustomDate();
}

Links

Todo

  • Sort those things a little bit

About

JavaScript is beautiful!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /