Skip to content

Instantly share code, notes, and snippets.

View cuneydbolukoglu's full-sized avatar
👻
He writes in his own way

Cüneyd BÖLÜKOĞLU cuneydbolukoglu

👻
He writes in his own way
View GitHub Profile
@cuneydbolukoglu
cuneydbolukoglu / themeChange.js
Last active February 19, 2023 11:14
changeTheme.js
/* Theme change */
let getTheme = localStorage.getItem("theme");
let link = document.createElement("link");
link.style = "text/css";
link.rel = "stylesheet";
if (getTheme == "red") {
link.href = './styles/red.css';
document.head.append(link);
} else {
link.href = './styles/blue.css';
@cuneydbolukoglu
cuneydbolukoglu / formatBytes.js
Created January 3, 2023 12:48
Convert Units
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Bytes';
var k = 1024,
dm = decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
// Usage:
@cuneydbolukoglu
cuneydbolukoglu / removeArrayDuplicates.js
Created March 29, 2022 20:40
How to Remove Array Duplicates in ES6
const data = [1, 2, 2, 3, 3, 4];
const removeArrayDuplicates = data.reduce(
(unique, item) => (unique.includes(item) ? unique : [...unique, item]),
[]
);
console.log(removeArrayDuplicates);
@cuneydbolukoglu
cuneydbolukoglu / inputfiltersearch.js
Created March 16, 2022 09:10
Input Filter Search Antdesign library 3.x
/**
* Order function by key.
* @default "asc"
* data => field to order (Object, List, Array).
* key => the data to be order by this key.
*/
function orderListByKey(data, key, order) {
const compareValues = (key, order = "asc") => (elemA, elemB) => {
if (!elemA.hasOwnProperty(key) || !elemB.hasOwnProperty(key)) return 0;
const comparison = elemA[key].localeCompare(elemB[key]);
@cuneydbolukoglu
cuneydbolukoglu / objectassignmap.js
Created March 16, 2022 09:08
Object assign map
this.state.data != undefined ? this.state.data.map(item => {
Object.assign({}, this.state.dataObject, { [item[0]]: item[1] });
console.log("dataObject", this.state.dataObject)
}) : undefined
@cuneydbolukoglu
cuneydbolukoglu / axiosApiToken.js
Created November 17, 2021 07:37
Axios Api Token
axios.defaults.xsrfCookieName = 'csrftoken';
axios.defaults.xsrfHeaderName = 'X-CSRFToken';
axios.defaults.headers.common['Cookie'] = document.cookie;
@cuneydbolukoglu
cuneydbolukoglu / reactObjectEntries.js
Last active March 18, 2022 19:05
React Object.entries()
const object = {
a: 'somestring',
b: 42
};
if(object){
for (const [key, value] of Object.entries(object)) {
console.log(`${key}: ${value}`);
}
} else {
@cuneydbolukoglu
cuneydbolukoglu / axiosDownload.js
Last active March 22, 2022 14:40
Download files with Axios
axios({
url: `/api/download/${id}/`,
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response], { type: response.type }));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', fileName);
link.target = '_blank'
@cuneydbolukoglu
cuneydbolukoglu / reactexamplestyling.jsx
Last active September 14, 2021 11:50
React example styling
import React from 'react';
import ReactDOM from 'react-dom';
import styles from './mystyle.module.css';
class Car extends React.Component {
render() {
return <h1 className={styles.bigblue}>Hello Car!</h1>;
}
}
class Car extends React.Component {
render() {
return <h2>I am a {this.props.brand}!</h2>;
}
}
class Garage extends React.Component {
render() {
return (
<div>