Skip to content

Instantly share code, notes, and snippets.

View fadziljusri's full-sized avatar

Fadzil Jusri fadziljusri

View GitHub Profile
@fadziljusri
fadziljusri / mobile_regex
Last active February 19, 2020 07:45
[Regex] Malaysian Mobile Phone Number
(\+?01)[0-46-9]-*[0-9]{7,8}
@fadziljusri
fadziljusri / axios.js
Last active February 14, 2020 02:41
axios plugin
import _axios from 'axios';
export const axios = _axios.create({
headers: {
'Content-Type': 'application/json;charset=UTF-8',
}
});
export const handleResponse = function (response) {
@fadziljusri
fadziljusri / catch.py
Created January 29, 2020 10:20
Python handle unexpected error and re-run app for retry counter
from time import sleep
do_error = None
def run(retry=0):
try:
print(do_error[1])
except Exception as e:
err_type = e.__class__.__name__
if err_type:

Http Responses for RESTful API

Helpers/middlewares to formats API responses into stuctured and human-readable way (especially for frontend dev)

Available language (at the moment..)

Example in Express Node.js

const { Res200 } = require("./httpResponses")
-- KENA BELANJA TONY ROMA'S NI HMMMMH 🤔 (AKU DAH SACRIFICE ALMOST HALF HOUR UTK KAU)
-- 1. Find the titles of all movies directed by Steven Spielberg.
SELECT title
FROM Movie
WHERE director = 'Steven Spielberg';
-- 2. Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order
@fadziljusri
fadziljusri / flatten_json.py
Created November 26, 2019 04:16
Flatten JSON object with nested keys into a single level.
def flatten_json(nested_json):
"""
Flatten json object with nested keys into a single level.
Args:
nested_json: A nested json object.
Returns:
The flattened json object if successful, None otherwise.
@fadziljusri
fadziljusri / API Reference.md
Created November 26, 2019 03:38
API Reference template

API Reference

Get API_TOKEN

API Description 1

Endpoint: https://some.api.com/1
Method: GET
headers: {"Authorization": "access_token API_TOKEN"}
@fadziljusri
fadziljusri / isDate.js
Created November 15, 2019 04:27
Date format validation
String.prototype.isDate = function () {
// let format = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/; // MM/DD/YYYY
let format = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/; // YYYY-MM-DD
return (format.test(this))
? new Date(this).toISOString() === this
: false
}
@fadziljusri
fadziljusri / index.js
Last active November 13, 2019 14:58
Desctruct Address component from input String
const DEFINED = require('./malaysia');
String.prototype.toTitleCase = function () {
return this.toLowerCase().replace(/\b(\w)/g, s => s.toUpperCase());
}
String.prototype.formatAdrressText = function() {
return this.replace(/[,.\n]/g, "") // remove unwanted chars
.toLowerCase()
.replace(new RegExp("malaysia", 'g'), "") // unnecessary
@fadziljusri
fadziljusri / xlsx.vue
Created October 4, 2019 07:04
Excel to JSON
<template>
<input ref="fileupload" type="file" @change="onFileChange" accept=".xlsx, .xls, .csv" />
</template>
<script>
/***
* Spreadsheet data toolkit https://github.com/SheetJS/js-xlsx
* npm i xlsx
*/
import XLSX from "xlsx";