Skip to content

Instantly share code, notes, and snippets.

View hackjutsu's full-sized avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / 7z_install.sh
Last active January 17, 2017 01:35
Steps to install 7z executable on Mac via Terminal.
# Homebrew
brew update
brew install p7zip
# Macport
sudo port install p7zip
@hackjutsu
hackjutsu / clearView.js
Last active January 17, 2017 04:10
Method to clear nodes inside a html element with specific id
// Clear all the nodes inside the element with id 'main-area'
function clearView() {
var mainArea = document.getElementById('main-area');
var firstChild = mainArea.firstChild;
while (firstChild) {
mainArea.removeChild(firstChild);
firstChild = mainArea.firstChild;
}
}
@hackjutsu
hackjutsu / object_own_keys_iteration.js
Last active January 18, 2017 03:00
Iteration on JS object's own keys.
let arr = ['a', 'b', 'c'];
Object.keys(arr).forEach(key => {
console.log(key)
});
@hackjutsu
hackjutsu / set_2_array.js
Last active January 22, 2017 05:35
Convert js Set to Array
// using Array.from
let array = Array.from(mySet);
// simply spreading the Set out in an array
let array = [...mySet];
@hackjutsu
hackjutsu / main_func.py
Last active January 23, 2017 06:45
[Define main() function for Python] We can run the script from terminal like: python main_func.py
# $ python main_func.py
def main():
print("Hello from main()")
if __name__ == '__main__':
main()
@hackjutsu
hackjutsu / html_encoding_decoding.py
Last active January 23, 2017 06:46
[HTML encoding/decoding via Python standard library] Reference: http://stackoverflow.com/a/7088472
# HTML Encoding
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
print(escape("<"))
# HTML Decoding
try:
@hackjutsu
hackjutsu / correct_implementation_equal_hashcode.java
Last active January 23, 2017 06:46
[Snippet for correct implementation of equals() and hashcode()] http://www.javaranch.com/journal/2002/10/equalhash.html
public class Test {
private int num;
private String data;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (obj.getClass() != this.getClass())) {
@hackjutsu
hackjutsu / simplest_Application.mk
Last active January 23, 2017 06:47
Simplest Application.mk for NDK
APP_PROJECT_PATH := <path to project>
@hackjutsu
hackjutsu / regex.sh
Last active January 23, 2017 06:48
[Example about doing regular expression match via Bash] Bash doesn't support non-greedy regex like (.*?)
str="asdkljfgaskdlgjkladsjfg123_abc_d4e5#asdfgerhgreg"
regex="[0-9]+_([a-z]+)_[0-9a-z]*"
if [[ $str =~ $regex ]]
then
name="${BASH_REMATCH[1]}"
echo "${name}.jpg" # 123_abc_d4e5.jpg
name="${name}.jpg" # same thing stored in a variable
else
echo "$str doesn't match" >&2
@hackjutsu
hackjutsu / find.sh
Last active January 24, 2017 20:10
Find command in Bash
find /path/to/dir -name "filename"
find /path/to/dir -name "*.h"