Skip to content

Instantly share code, notes, and snippets.

View truongluu's full-sized avatar
🏠
Working from home

Lưu Xuân Trường truongluu

🏠
Working from home
View GitHub Profile
@truongluu
truongluu / App.test.ts
Created July 23, 2024 02:31 — forked from mustafadalga/App.test.ts
Mocking localstorage in vitest
import { createStore, Store } from 'vuex';
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { key, store } from "@/store";
import type { User } from "@/types/types";
import App from "@/App.vue";
import { DOMWrapper, mount, VueWrapper } from "@vue/test-utils";
import type { State } from "@/store"
const localStorageMock: Storage = (() => {
let store: Record<string, string> = {};
@truongluu
truongluu / docker-compose.yml
Created May 15, 2024 15:27 — forked from drdogbot7/docker-compose.yml
Wordpress Docker Compose with CLI - official images
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
ports:
- "[YOUR_DESIRED_SQL_PORT]:3306"
environment:
MYSQL_ROOT_PASSWORD: wordpress
@truongluu
truongluu / cachedFetch.js
Created December 18, 2023 09:14 — forked from mritzco/cachedFetch.js
Javascript caching fetch data
// From https://www.sitepoint.com/cache-fetched-ajax-requests/
// All credits to: Peter Bengtsson
// Added Content-type to headers so it can go to traditional validation like fetch does
// Add some debugging messages: activate with { verbose: true }
// Add a request to be able to add headers instead of just calling URL
const CachedFetch = (url, options) => {
let expiry = options.seconds || 5 * 60 // 5 min default
let logger = (options.verbose) ? console.log : function(){};
@truongluu
truongluu / wc-sortable-custom-taxonomy.php
Created November 22, 2023 11:48 — forked from RadGH/wc-sortable-custom-taxonomy.php
Woocommerce - Sortable custom taxonomy
<?php
// This is so easy, it's embarassing that I took 20 minutes to find it.
// "Catalog" is a custom taxonomy.
function make_catalog_sortable( $sortables ) {
$sortables[] = 'catalog';
return $sortables;
}
add_filter( 'woocommerce_sortable_taxonomies', 'make_catalog_sortable' );
@truongluu
truongluu / create_order.php
Created November 11, 2023 08:54 — forked from shitpoet/create_order.php
Programmatically create order, add (variative) products from cart to it, change status to `processing` and empty cart. Wordpress, WooCommerce 2.4.
$address = array(
'first_name' => $customer_name,
'last_name' => '',
'company' => '',
'email' => $customer_email,
'phone' => $customer_phone,
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
@truongluu
truongluu / SubmitFormHandler.tsx
Last active April 15, 2023 12:18
How to type a React Form with submit handler
import React from 'react';
export type GenHTMLFormControlsCollection<T> = HTMLFormControlsCollection & {
[inputName in keyof T]: HTMLInputElement;
};
interface GenericFormElement<T extends { [inputName: string]: HTMLInputElement }>
extends HTMLFormElement {
readonly elements: GenHTMLFormControlsCollection<T>;
}
@truongluu
truongluu / package.json
Created December 9, 2022 08:46
Gulp WP Theme - Package
{
"name": "wpsitename",
"version": "1.0.0",
"description": "",
"author": "jacekolczak",
"private": true,
"devDependencies": {
"gulp": "latest",
"gulp-livereload": "latest",
"gulp-sass": "latest",
@truongluu
truongluu / gulpfile.js
Created December 9, 2022 08:45
Gulp configs for WP
var gulp = require('gulp'),
sass = require('gulp-sass')(require('sass')),
livereload = require('gulp-livereload'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
dest = require('gulp-dest'),
order = require('gulp-order'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer')
rename = require('gulp-rename'),
@truongluu
truongluu / compose-function.js
Created May 16, 2022 04:54
Compose function
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
@truongluu
truongluu / curry-function.js
Created May 15, 2022 08:33
Curry function
/***
* function sum(a,b,c) { return a +b + c;}
* const cSum = curry(sum)
* cSum(1, 2, 3) => 6
* cSum(1)(2)(3) => 6
* cSum(1)(2,3) => 6
*/
function curry(func) {
return (...args) => {