Skip to content

Instantly share code, notes, and snippets.

@wiyoe
wiyoe / random.js
Created January 4, 2018 13:52
Javascript random numbers.
// Naive utility method
function getRandomNumber(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomNumber(15, 20);
// Lodash
_.random(15, 20);
@wiyoe
wiyoe / reduce.js
Created January 5, 2018 06:42
Javascript array reduce example.
var rockets = [
{ country:'Russia', launches:32 },
{ country:'US', launches:23 },
{ country:'China', launches:16 },
{ country:'Europe(ESA)', launches:7 },
{ country:'India', launches:4 },
{ country:'Japan', launches:3 }
];
@wiyoe
wiyoe / banana.js
Created January 11, 2018 12:00
Banana Text with JS
'b' + 'a' + + 'a' + 'a' // "baNaNa"
@wiyoe
wiyoe / url-encoding.js
Created January 12, 2018 08:41
Javascript URL Encoding
function param(object) {
var encodedString = '';
for (var prop in object) {
if (object.hasOwnProperty(prop)) {
if (encodedString.length > 0) {
encodedString += '&';
}
encodedString += encodeURI(prop + '=' + object[prop]);
}
}
@wiyoe
wiyoe / xml-http-request.js
Created January 12, 2018 08:43
Sending and Receiving JSON- Native
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'myservice/user/1234');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
var userInfo = JSON.parse(xhr.responseText);
}
};
xhr.send(JSON.stringify({
name: 'John Smith',
@wiyoe
wiyoe / mobile-phone.js
Last active March 14, 2022 09:44
Turkey Mobile Phone Regex
var str = "5359999999";
var patt = new RegExp("(05|5)[0-9][0-9][1-9]([0-9]){6}");
var res = patt.test(str);
console.log(res); // true
@wiyoe
wiyoe / class.database.php
Created January 15, 2018 08:44
PHP OOP Database class using MySQLI and Singleton pattern. Only one instance of the class will be made, this requires less memory.
<?php
/*
* Mysql database class - only one connection alowed
*/
class Database {
private $_connection;
private static $_instance; //The single instance
private $_host = "HOSTt";
private $_username = "USERNAME";
private $_password = "PASSWORd";
@wiyoe
wiyoe / timestamp.js
Last active January 18, 2018 06:38
Javascript Timestamp(seconds)
var d = new Date();
var seconds = Math.round(d.getTime() / 1000); // 10 digit
// Same as moment.js
moment().unix();
@wiyoe
wiyoe / lodash-groupby-count.js
Created January 23, 2018 07:30
Loadash groupBy Count
var dd = [
{"classroom_fk":9,"classroom_name":"9. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":10,"classroom_name":"10. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":11,"classroom_name":"11. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":11,"classroom_name":"11. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":11,"classroom_name":"11. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":12,"classroom_name":"12. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":12,"classroom_name":"12. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":12,"classroom_name":"12. Sınıf","maxLicenceNumber":100,"licenceCount":6,"pointtype_related":true},
{"classroom_fk":10,"classroom_name":"10. Sınıf","maxLicenceNumber":100,"licen
@wiyoe
wiyoe / remove-node-modules.txt
Created January 26, 2018 10:24
Remove node_modules folder from git.
#add 'node_modules' to .gitignore file
git rm -r --cached node_modules
git commit -m "Remove the now ignored directory node_modules"
git push origin master