Skip to content

Instantly share code, notes, and snippets.

https://www.briangonzalez.org/post/copying-array-javascript

const names = [ 'Jon', 'Jacob', 'Jeff' ]

const copy1 = names.slice() const copy2 = [].concat(names) const copy3 = Object.values(names) const copy4 = [...names] const copy5 = Array.of(...names) const copy6 = JSON.parse(JSON.stringify(names))

@oleksapro
oleksapro / apache-redirect-uppercase-to-lowercase
Created January 12, 2018 10:38
Redirect in Apache url to lowercase
RewriteBase /
RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}} [L,R=301]
@oleksapro
oleksapro / random range
Last active April 23, 2017 21:03
random range
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
function bubbleSort(a)
{
var swapped;
do {
swapped = false;
for (var i=0; i < a.length-1; i++) {
if (a[i] > a[i+1]) {
var temp = a[i];
function str_preview( $str, $max = 300 ){
$end = ' ...';
$end_length = mb_strlen($end);
$content = $str;
$content = strip_tags($content);
$content = ltrim($content);
while( '&nbsp;' == mb_substr($content, 0, 6) ){
$content = mb_substr( $content, 6 );
$content = ltrim($content);
}
function FileSizeConvert($bytes){
$bytes = floatval($bytes);
$arBytes = array(
0 => array(
"UNIT" => "Tб",
"VALUE" => pow(1024, 4)
),
1 => array(
"UNIT" => "Гб",
function _n_ru($text_one,$text_two,$text_def,$number){
$modulo_100 = $number % 100;
$modulo_10 = $number % 10;
if( 10 != $modulo_100 - $modulo_10 ){
switch( $modulo_10 ){
case 1:
return $text_one;
case 2:
case 3:
case 4:
@oleksapro
oleksapro / replace text in html
Created October 21, 2016 10:41
jquery, text, html
$('p').html(function(_, text) {
return text.replace('<!--more-->', '<a href="#" class="read_more">read more</a>' );
});
if (window.innerWidth > 640) {
var floatParentNode = document.querySelector('.js-parent');
var floatNode = document.querySelector('.js-float');
var valScrollForFixed = floatParentNode.getBoundingClientRect().top + window.pageYOffset - document.querySelector('.js-header').offsetHeight;
window.addEventListener('scroll', function () {
if (window.innerHeight > floatNode.offsetHeight) {
var windowScrollTop = window.pageYOffset;
var footerTop = document.querySelector('.js-footer').getBoundingClientRect().top + windowScrollTop;
var footerHeight = document.querySelector('.js-footer').clientHeight;
// http://learn.javascript.ru/external-script
function addScript(src){
var script = document.createElement('script');
script.src = src;
script.async = false; // чтобы гарантировать порядок
document.head.appendChild(script);
}
addScript('1.js'); // загружаться эти скрипты начнут сразу