Skip to content

Instantly share code, notes, and snippets.

View lai32290's full-sized avatar

Lai Xuancheng lai32290

View GitHub Profile
@lai32290
lai32290 / Array Flat
Last active May 19, 2016 02:11
Array Flat
Array.prototype.flat = function () {
var result = [];
this.forEach(function (item) {
if (Array.isArray(item)) {
result = result.concat(item.flat());
return;
}
result.push(item);
@lai32290
lai32290 / Array Flat Function
Created May 19, 2016 02:40
Array Flat Function
var arrayFlat = function (arr) {
var result = [];
arr.forEach(function (item) {
if(Array.isArray(item)) {
result = result.concat(toArray(item));
return;
}
result.push(item);
});
@lai32290
lai32290 / dynamicForm.js
Created August 19, 2016 17:57
A lib to create dynamic form to submit
function DynamicForm(url)
{
var self = this;
var fields = [];
self.target = "";
self.method = "get";
self.action = url;
self.addField = function(name, value) {
fields.push({name: name, value: value});
@lai32290
lai32290 / proxy.pac
Created May 8, 2017 11:10
Ubuntu auto detect proxy pac config file
function FindProxyForURL(url, host)
{
if (isInNet(myIpAddress(), "1.2.3.0", "255.255.255.0")) {
if (isInNet(host, "192.168.0.0", "255.255.0.0"))
return "DIRECT";
if (shExpMatch(url, "http:*"))
return "PROXY my.proxy.com:8000" ;
if (shExpMatch(url, "https:*"))
return "PROXY my.proxy.com:8000" ;
if (shExpMatch(url, "ftp:*"))
@lai32290
lai32290 / WordPress Theme Customizer Sample.php
Created September 3, 2017 13:57 — forked from Abban/WordPress Theme Customizer Sample.php
WordPress Theme Customizer Sample
<?php
function themename_customize_register($wp_customize){
$wp_customize->add_section('themename_color_scheme', array(
'title' => __('Color Scheme', 'themename'),
'priority' => 120,
));
// =============================
@lai32290
lai32290 / functionReturnSpec.js
Last active October 18, 2017 19:39
Javascript Unit Test Quick Guide - functionReturnSpec.js
import { sum } from "./myMathLib.js";
describe("Testando retorno da funcao", () => {
it("deve retornar a soma dos valores 1 e 2", () => {
const result = sum(1, 2); // pegando resultado da soma de 1 e 2
expect(result).toEqual(3); // verificar se o resultado retornado é igual a 3
});
});
@lai32290
lai32290 / javascriptUnitTest.md
Last active November 5, 2017 19:35
Javascript Unit Test

Público Alvo

Programadores familiarizado com Javascript, porém sem/pouca experiencia em unit test.

O que Será Abordado

Esse post irá abordar vários casos de testes e como eles podem ser testados, de uma forma bem direta e objetiva.

Testes

Como hoje existe vários bibliotecas para realizar unit test, cada um tem suas características, os exemplos usado neste artigo será usado Jasmine, porém todos eles podem ser realizados em outras bibliotecas de unit test também.

Retorno da função está correto?

const Nightmare = require('nightmare')
const nightmare = Nightmare({ show: true })
nightmare
.goto('http://blablablablabla/logon/logon.asp')
.wait(() => {
return document.querySelector('[name=username]') !== null;
})
.evaluate(() => {
document.querySelector('[name=username]').value = "visitante";
@lai32290
lai32290 / Observer Class in C#
Created March 24, 2018 13:42
Observer Class
public class Observer<T>
{
public Observer() { }
public Observer(T initValue)
{
value = initValue;
}
private T value;
// accordion.js
import React from 'react'
import AccordionContents from './accordion-contents'
class Accordion extends React.Component {
state = {openIndex: 0}
setOpenIndex = openIndex => this.setState({openIndex})
render() {
const {openIndex} = this.state
return (
<div>