Skip to content

Instantly share code, notes, and snippets.

View klauszhang's full-sized avatar

Hao Zhang klauszhang

View GitHub Profile
@klauszhang
klauszhang / migrate.js
Last active July 31, 2019 01:13
Migrate gitlab variables
// read value
JSON.stringify(
[...document.getElementsByClassName('js-row')].map(el => {
const key = el.querySelector('.js-ci-variable-input-key').value
const { value } = el.querySelector('textarea.js-ci-variable-input-value')
return [key, value]
})
)
const result=[[]] //whatever from last step
@klauszhang
klauszhang / extract.js
Last active May 23, 2019 04:01
Extract aws config from eb
const val = Array.from(document.getElementsByTagName('tbody')[1].children)
.filter(ele => ele.className === 'ng-scope')
.map(element => ({
[element.children[0].children[0].value]:
element.children[1].children[0].value,
}))
.filter(value => Object.keys(value)[0])
.reduce((curr, acc) => ({ ...curr, ...acc }), {})
JSON.stringify(val)
const getSolution = require('./dic-lookup');
const digit = 4;
const count = 1000;
const sut = getSolution(digit, count)();
// should have 1000 elements
if (sut.size !== count) {
console.log(sut.size);
console.error('something goes wrong...');
@klauszhang
klauszhang / dic-lookup.js
Created November 13, 2017 20:05
the dic look up approach
function maxLength(digit) {
return Math.pow(8, digit) * 9;
}
const getDic = digit =>
Array.from(new Array(Math.pow(10, digit) - 1))
.map((_, idx) => idx + 1)
.filter(v => v >= Math.pow(10, digit - 1));
function random(max) {
return Math.floor(Math.random() * (max + 1));
const {
generator,
getSolution
} = require('./refined');
const digit = 7;
const count = 10000;
const sut = getSolution(count)(
generator(digit)
)();
function getNumber(digit = 4) {
return () =>
Math.floor(
Math.random() * Math.pow(10, digit)
);
}
function getNumInRange(digit = 4) {
return function(numberGenerator) {
return function recursiveQuery() {
@klauszhang
klauszhang / test.js
Last active November 13, 2017 07:21
a validation
//1357 just a random number to get thing started
const sut = require('./index')(1357);
// should have 1000 elements
if (sut.size !== 1000) {
console.log(sut.size);
console.error('something goes wrong...');
}
@klauszhang
klauszhang / solution-functional.js
Last active November 13, 2017 07:17
a functional appropach
function getNumber() {
return Math.floor(Math.random() * 10000);
}
function getFourDigitNum(pin) {
if (pin <= 1000) {
getFourDigitNum(getNumber());
}
return pin;
@klauszhang
klauszhang / solution.js
Last active November 13, 2017 06:06
a trail for code
function resolve(howMany) {
const arr = new Set();
// generate stuff
function getNumber() {
return Math.floor(Math.random() * 10000);
}
function getFourDigitNum() {
let pin;
@klauszhang
klauszhang / eslint-common.js
Created August 23, 2017 09:37
for common linting
module.exports = {
parser: 'babel-eslint',
env: {
commonjs: true,
es6: true,
node: true,
jest: true
},
extends: ['eslint:recommended'],
parserOptions: {