Skip to content

Instantly share code, notes, and snippets.

View yavgel85's full-sized avatar

Eugene Yavgel yavgel85

View GitHub Profile
@yavgel85
yavgel85 / indexOfSubstrings.js
Created March 26, 2021 11:45
indexOfSubstrings #js #algorithm
// 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);
@yavgel85
yavgel85 / isAbsoluteURL.js
Created March 26, 2021 11:35
isAbsoluteURL #js
// 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
@yavgel85
yavgel85 / URLJoin.js
Created March 26, 2021 11:33
URLJoin #js
// 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://')
@yavgel85
yavgel85 / getBaseURL.js
Created March 26, 2021 11:31
getBaseURL #js #browser
// 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'
@yavgel85
yavgel85 / getURLParameters.js
Created March 26, 2021 11:29
getURLParameters #js #browser
// 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
@yavgel85
yavgel85 / When registering routes, you may limit the possible values for a route parameter.php
Created March 26, 2021 08:44
When registering routes, you may limit the possible values for a route parameter #laravel #route
<?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')
;
@yavgel85
yavgel85 / UrlFormatter.php
Created March 26, 2021 08:42
Url Formatter #laravel #php #helper
// Format a string into a url and translate any characters based on a conversion table
//UrlFormatter
<?php
namespace AppBundle\Formatter;
class UrlFormatter
{
@yavgel85
yavgel85 / TruncateNumber.php
Created March 26, 2021 08:40
Truncate Number #php
<?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;
}
@yavgel85
yavgel85 / Transform brackets into an Eloquent query.php
Created March 26, 2021 08:39
Transform brackets into an Eloquent query #laravel #db
<?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');
@yavgel85
yavgel85 / Size ordering helper.php
Created March 26, 2021 08:37
Size ordering helper #laravel #helper
<?php
namespace AppBundle\Helper;
class SizeOrderHelper
{
private const PATTERNS = [
'^[0-9]+$' => false,
'^[0-9\/]+$' => false,
'^[0-9]+yrs$' => false,