This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finds all the indexes of a substring in a given string. | |
// Use Array.prototype.indexOf() to look for searchValue in str. | |
// Use yield to return the index if the value is found and update the index, i. | |
// Use a while loop that will terminate the generator as soon as the value returned from Array.prototype.indexOf() is -1. | |
const indexOfSubstrings = function* (str, searchValue) { | |
let i = 0; | |
while (true) { | |
const r = str.indexOf(searchValue, i); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks if the given string is an absolute URL. | |
// Use RegExp.prototype.test() to test if the string is an absolute URL. | |
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str); | |
// Examples | |
isAbsoluteURL('https://google.com'); // true | |
isAbsoluteURL('ftp://www.myserver.net'); // true | |
isAbsoluteURL('/foo/bar'); // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Joins all given URL segments together, then normalizes the resulting URL. | |
// Use String.prototype.join('/') to combine URL segments. | |
// Use a series of String.prototype.replace() calls with various regexps to normalize the resulting URL (remove double slashes, add proper slashes for protocol, remove slashes before parameters, combine parameters with '&' and normalize first parameter delimiter). | |
const URLJoin = (...args) => | |
args | |
.join('/') | |
.replace(/[\/]+/g, '/') | |
.replace(/^(.+):\//, '$1://') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gets the current URL without any parameters or fragment identifiers. | |
// Use String.prototype.replace() with an appropriate regular expression to remove everything after either '?' or '#', if found. | |
const getBaseURL = url => url.replace(/[?#].*$/, ''); | |
// Examples | |
getBaseURL('http://url.com/page?name=Adam&surname=Smith'); // 'http://url.com/page' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creates an object containing the parameters of the current URL. | |
// Use String.prototype.match() with an appropriate regular expression to get all key-value pairs. | |
// Use Array.prototype.reduce() to map and combine them into a single object. | |
// Pass location.search as the argument to apply to the current url. | |
const getURLParameters = url => | |
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( | |
(a, v) => ( | |
(a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// When registering routes, you may limit the possible values for a route parameter. In this way you may change your response based on a valid parameter, having an expressive route and not a Query string parameter. | |
Route::get('stores/{store}/orders/{type?}') | |
->uses('StoreOrderController@byType') | |
->name('stores.orders.by-type.index') | |
->where('type', 'new|fulfilling|pickup|history') | |
; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Format a string into a url and translate any characters based on a conversion table | |
//UrlFormatter | |
<?php | |
namespace AppBundle\Formatter; | |
class UrlFormatter | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Truncate Number without rounding the value | |
function truncate_number($number, $precision = 2) | |
{ | |
// Zero causes issues, and no need to truncate | |
if (0 == (int) $number) { | |
return $number; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// What if you have and-or mix in your SQL query, like this: | |
// ... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65) How to translate it into Eloquent? | |
// This is the wrong way: | |
$q->where('gender', 'Male'); | |
$q->orWhere('age', '>=', 18); | |
$q->where('gender', 'Female'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Helper; | |
class SizeOrderHelper | |
{ | |
private const PATTERNS = [ | |
'^[0-9]+$' => false, | |
'^[0-9\/]+$' => false, | |
'^[0-9]+yrs$' => false, |