├── Makefile ├── README.md ├── bash ├── string-equals ├── test-directory-exists ├── test-executable-exists └── test-file-exists ├── go ├── bytes-to-string ├── channel-range ├── defer ├── directory-list ├── directory-walk ├── file-read ├── file-write ├── float-to-int ├── float-to-string ├── function ├── function-closure ├── function-type ├── http-get ├── http-get-json ├── http-post ├── http-post-json ├── if ├── if-else ├── if-else-if ├── if-err ├── if-err-initialize ├── import ├── int-random ├── int-to-float ├── int-to-string ├── interface ├── json-marshal ├── json-unmarshal ├── loop-for ├── loop-for-initialize ├── loop-range ├── main-scaffold ├── map ├── map-if-ok ├── mutex ├── pattern-decorator ├── pattern-factory ├── pattern-semaphor ├── regex-test ├── select ├── slice-append ├── slice-capacity ├── slice-combine ├── slice-element-random ├── slice-init ├── slice-length ├── slice-pop ├── slice-prepend ├── slice-reverse ├── slice-shift ├── slice-shuffle ├── slice-slice ├── slice-sort ├── slice-spread ├── slice-to-string ├── slice-unshift ├── sql-database-connect ├── sql-exec ├── sql-exec-prepared-statement ├── sql-prepare-statement ├── sql-query ├── sql-query-prepared-statement ├── sql-run ├── sql-run-prepared-statement ├── sql-transaction-prepare-statement ├── sqlx-database-connect ├── sqlx-query-prepared-statement ├── sqlx-select ├── stderr-log ├── stderr-write ├── stdin-arguments-read ├── stdin-read-line ├── stdin-read-loop ├── stdout-log ├── stdout-write ├── string-split ├── string-to-float ├── string-to-integer ├── string-to-lowercase ├── string-to-readable ├── string-to-time ├── string-to-uppercase ├── string-trim ├── struct ├── struct-anonymous ├── struct-anonymous-slice ├── struct-method ├── struct-scaffold ├── switch ├── switch-no-condition ├── system-execute ├── test-bench ├── test-function ├── test-scaffold ├── time-add-subtract ├── time-get-year-month-day ├── timestamp-create ├── timestamp-create-unix ├── timestamp-format ├── type-assert ├── url-parse ├── var-declare ├── var-initialize ├── waitgroup ├── yaml-marshal └── yaml-unmarshal ├── js ├── array-append ├── array-concat ├── array-every ├── array-filter ├── array-includes ├── array-initialize ├── array-length ├── array-map ├── array-pop ├── array-prepend ├── array-push ├── array-reverse ├── array-shift ├── array-some ├── array-sort ├── array-to-string ├── array-to-string-join ├── function ├── function-closure ├── function-memoize ├── if ├── if-else ├── if-else-if ├── json-serialize ├── json-unserialize ├── loop-do-while ├── loop-for ├── loop-for-of ├── loop-foreach ├── loop-while ├── number-absolute-value ├── number-exponentation ├── number-is-a-number ├── number-is-not-a-number ├── number-maximum ├── number-minimum ├── number-random ├── number-round ├── number-round-down ├── number-round-up ├── object-initialize ├── object-keys ├── string-includes ├── string-interpolate ├── string-length ├── string-replace ├── string-reverse ├── string-substring ├── string-to-array ├── string-to-float ├── string-to-int ├── string-to-lowercase ├── string-to-uppercase ├── string-trim ├── switch ├── ternary ├── throw ├── try-catch └── try-catch-finally ├── lua ├── function ├── function-closure ├── function-varargs ├── if ├── if-else ├── if-else-if ├── loop-for ├── loop-ipairs ├── loop-pairs ├── loop-repeat ├── loop-while ├── string-length ├── switch ├── table-append ├── table-array-init ├── table-constructor ├── table-insert ├── table-length ├── table-method ├── table-object-init ├── table-prepend ├── table-remove └── ternary ├── markdown ├── code-block ├── code-inline ├── heading-h1 ├── heading-h2 ├── heading-h3 ├── heading-h4 ├── heading-h5 ├── heading-h6 ├── horizontal-line ├── image ├── image-link ├── image-reference ├── link-inline ├── link-mailto ├── link-reference ├── list-ordered ├── list-unordered ├── quote-block ├── quote-block-nested ├── table ├── text-bold ├── text-italic └── text-strikethrough ├── nodejs ├── csv-read ├── csv-write ├── directory-list ├── directory-list-sync ├── directory-walk ├── file-read ├── file-read-sync ├── file-write ├── file-write-sync ├── http-get ├── http-get-json ├── http-post ├── http-post-json ├── import ├── process-exit ├── require ├── stderr-log ├── stderr-write ├── stdin-read-bytes ├── stdin-read-line ├── stdin-read-loop ├── stdin-read-string ├── stdout-log └── stdout-write ├── php ├── array-concat ├── array-join ├── array-key-exists ├── array-keys ├── array-map ├── array-pop ├── array-push ├── array-reverse ├── array-shift ├── array-slice ├── array-sort ├── array-spread ├── array-to-object ├── array-unshift ├── array-values ├── class-function ├── class-scaffold ├── directory-list ├── directory-walk ├── file-read ├── file-write ├── function ├── function-closure ├── if ├── if-else ├── if-else-if ├── json-serialize ├── json-unserialize ├── loop-do-while ├── loop-for ├── loop-foreach ├── loop-while ├── null-coalesce ├── pattern-singleton ├── regex-test ├── require ├── string-heredoc ├── string-lowercase ├── string-nowdoc ├── string-split ├── string-uppercase ├── switch ├── ternary ├── test-function └── test-scaffold └── sql ├── create-table-sqlite3 ├── database-drop ├── index-create ├── index-drop ├── insert ├── insert-multiple ├── join-inner ├── join-outer ├── join-right-outer ├── select ├── table-drop ├── table-truncate ├── transaction-commit ├── transaction-start └── update /Makefile: -------------------------------------------------------------------------------- 1 | # makefile path 2 | makefile := $(realpath $(lastword $(MAKEFILE_LIST))) 3 | 4 | # executables 5 | CAT := cat 6 | COLUMN := column 7 | GREP := grep 8 | SED := sed 9 | SORT := sort 10 | 11 | ## todo: list stubbed snippet files 12 | .PHONY: todo 13 | todo: 14 | @$(GREP) -l --exclude=Makefile --exclude=README.md -r TODO | $(SORT) 15 | 16 | ## help: display this help text 17 | .PHONY: help 18 | help: 19 | @$(CAT) $(makefile) | \ 20 | $(SORT) | \ 21 | $(GREP) "^##" | \ 22 | $(SED) 's/## //g' | \ 23 | $(COLUMN) -t -s ':' 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | snippets 2 | ======== 3 | Language snippets for use with [vim-cheat][]. 4 | 5 | Creating Snippets 6 | ----------------- 7 | Keep the following suggestions in mind when creating snippets: 8 | 9 | ### Snippets should be task-focused ### 10 | The primary goal of [vim-cheat][] is not to save keystrokes, but rather to 11 | lessen the _mental effort_ required to complete a task. With that said, don't 12 | constrain your snippets in arbitrary ways, like "one function per snippet". 13 | Write as much code as it takes to document a task. 14 | 15 | Recalling programming solutions is hard, but deleting boilerplate is easy. 16 | Ensure that your snippets contain the information you need, and work to sever 17 | your reliance on search engines. 18 | 19 | ### Strive for brevity ### 20 | With that said, do strive for brevity where possible. As an example, prefer 21 | using descriptive variable and function names over code comments. 22 | 23 | ### Tag snippets liberally ### 24 | Tag snippets with whichever "keywords" you might enter into a search-engine 25 | when researching a problem. Use synonyms as necessary. 26 | 27 | Perhaps you're creating a snippet about establishing a database connection. How 28 | might you tag it? Perhaps: 29 | 30 | - `connection` 31 | - `create` 32 | - `database` 33 | - `db` 34 | - `establish` 35 | - `open` 36 | 37 | Liberal tagging increases the likelihood that you're able to find a snippet 38 | when you need it. 39 | 40 | ### Be mindful of tag substrings ### 41 | Should you also tag the snippet above with `connect`? No. Be mindful of 42 | substrings. 43 | 44 | The purpose of tagging snippets is to give [fzf][] text to match against. That 45 | being the case, avoid using tags that are substrings of other tags. Given that 46 | `connect` is a substring of `connection`, the former will naturally match the 47 | latter, and thus needn't be applied as a tag. 48 | 49 | ### Strive for consistency among languages 50 | Use the same tags for the same tasks when writing snippets for multiple 51 | languages. Doing so will allow you to develop a "meta-language" that persists 52 | across language boundaries. 53 | 54 | 55 | [fzf]: https://github.com/junegunn/fzf.vim 56 | [vim-cheat]: https://github.com/cheat/vim-cheat 57 | -------------------------------------------------------------------------------- /bash/string-equals: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: bash 3 | tags: 4 | - conditional 5 | - equals 6 | - if 7 | - string 8 | - test 9 | --- 10 | if [ "$foo" == "bar" ]; then 11 | # var `foo` equals "bar" 12 | fi 13 | -------------------------------------------------------------------------------- /bash/test-directory-exists: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: bash 3 | tags: 4 | - conditional 5 | - directory 6 | - exists 7 | - if 8 | - test 9 | --- 10 | if [ -d "$DIRECTORY" ]; then 11 | # the directory exists 12 | fi 13 | -------------------------------------------------------------------------------- /bash/test-executable-exists: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: bash 3 | tags: 4 | - conditional 5 | - exists 6 | - file 7 | - if 8 | - test 9 | --- 10 | # test if command `foo` is available on the `$PATH` 11 | if ! [ -x "$(command -v foo)" ]; then 12 | # the `foo` executable is not on the path 13 | fi 14 | -------------------------------------------------------------------------------- /bash/test-file-exists: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: bash 3 | tags: 4 | - conditional 5 | - exists 6 | - file 7 | - if 8 | - test 9 | --- 10 | if [ -f "$FILEPATH" ]; then 11 | # the file exists 12 | fi 13 | -------------------------------------------------------------------------------- /go/bytes-to-string: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - bytes 5 | - coerce 6 | - convert 7 | - go 8 | - string 9 | --- 10 | str := string(myBytes) 11 | -------------------------------------------------------------------------------- /go/channel-range: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - channel 5 | - go 6 | - iterate 7 | - loop 8 | - range 9 | --- 10 | // initialize a queue channel 11 | queue := make(chan string, 2) 12 | 13 | // push items into the queue 14 | queue <- "foo" 15 | queue <- "bar" 16 | 17 | // close the queue to prevent deadlock 18 | close(queue) 19 | 20 | for item := range queue { 21 | fmt.Println(item) 22 | } 23 | -------------------------------------------------------------------------------- /go/defer: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/directory-list: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/directory-walk: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/file-read: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - file 5 | - go 6 | - io 7 | - read 8 | --- 9 | bytes, err := ioutil.ReadFile("/path/to/file") 10 | if err != nil { 11 | // log.Fatalf("failed to read file: %v", err) 12 | } 13 | 14 | // coerce `[]byte` to `string` if necessary 15 | // fmt.Println(string(bytes)) 16 | -------------------------------------------------------------------------------- /go/file-write: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - file 5 | - go 6 | - io 7 | - write 8 | --- 9 | if err := ioutil.WriteFile("/tmp/data.txt", []byte("hello world"), 0644); err != nil { 10 | // log.Fatalf("failed to write file: %v", err) 11 | } 12 | -------------------------------------------------------------------------------- /go/float-to-int: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/float-to-string: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - coerce 5 | - convert 6 | - float 7 | - go 8 | - string 9 | --- 10 | s := fmt.Sprintf("%f", 123.45) // do not truncate decimals, or 11 | s := fmt.Sprintf("%.2f", 123.45) // truncate to 2 decimal places 12 | -------------------------------------------------------------------------------- /go/function: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - function 5 | - go 6 | --- 7 | func add(x int, y int) (int, error) { 8 | return x+y, nil 9 | } 10 | -------------------------------------------------------------------------------- /go/function-closure: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - anonymous 5 | - closure 6 | - function 7 | - go 8 | - lambda 9 | --- 10 | fn := func(x int) bool { 11 | return x > 10000 12 | } 13 | -------------------------------------------------------------------------------- /go/function-type: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - function 5 | - go 6 | - struct 7 | - type 8 | --- 9 | type Foo func(string, string) (string, error) 10 | -------------------------------------------------------------------------------- /go/http-get: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/go/http-get -------------------------------------------------------------------------------- /go/http-get-json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/go/http-get-json -------------------------------------------------------------------------------- /go/http-post: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/go/http-post -------------------------------------------------------------------------------- /go/http-post-json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/go/http-post-json -------------------------------------------------------------------------------- /go/if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - conditional 5 | - control 6 | - go 7 | --- 8 | if true { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /go/if-else: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - conditional 5 | - control 6 | - go 7 | --- 8 | if true { 9 | 10 | } else { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /go/if-else-if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - conditional 5 | - control 6 | - go 7 | --- 8 | if true { 9 | 10 | } else if true { 11 | 12 | } else { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /go/if-err: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - control 5 | - error 6 | - go 7 | --- 8 | if err != nil { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /go/if-err-initialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - control 5 | - error 6 | - go 7 | - initialize 8 | --- 9 | if err := fn(); err != nil { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /go/import: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - dependency 5 | - go 6 | - import 7 | - module 8 | - require 9 | --- 10 | import ( 11 | "fmt" 12 | ) 13 | -------------------------------------------------------------------------------- /go/int-random: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - create 5 | - get 6 | - go 7 | - integer 8 | - number 9 | - random 10 | --- 11 | // seed the random number generator 12 | // NB: this seed is not cryptographically random. Use the `crypto_rand` package 13 | // if you need cryptographic entropy. 14 | rand.Seed(time.Now().Unix()) 15 | r := rand.Intn(5) // return 0-4; set is [0,n) 16 | -------------------------------------------------------------------------------- /go/int-to-float: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - coerce 5 | - convert 6 | - float 7 | - go 8 | - int 9 | --- 10 | f := float64(10) 11 | -------------------------------------------------------------------------------- /go/int-to-string: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - coerce 5 | - convert 6 | - go 7 | - int 8 | - string 9 | --- 10 | s := strconv.Itoa(100) 11 | -------------------------------------------------------------------------------- /go/interface: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - interface 6 | - type 7 | --- 8 | type Car interface { 9 | Speed() float64 10 | Honk() string 11 | } 12 | -------------------------------------------------------------------------------- /go/json-marshal: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - encode 5 | - go 6 | - json 7 | - marshal 8 | - serialize 9 | - struct 10 | --- 11 | /* Marshal a struct into a JSON string */ 12 | 13 | // define a struct to marshal 14 | type User struct { 15 | FirstName string `json:"firstname"` 16 | LastName string `json:"lastname"` 17 | } 18 | 19 | // initialze a struct 20 | user := User{ 21 | FirstName: "John", 22 | LastName: "Smith", 23 | } 24 | 25 | // marshal the struct into a slice of bytes 26 | bs, err := json.Marshal(&user) 27 | if err != nil { 28 | // log.Fatalf("failed to marshal json: %v", err) 29 | } 30 | 31 | // coerce the slice of bytes to a string 32 | // fmt.Println(string(bs)) 33 | -------------------------------------------------------------------------------- /go/json-unmarshal: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - decode 5 | - go 6 | - json 7 | - parse 8 | - struct 9 | - unmarshal 10 | - unserialize 11 | --- 12 | /* Unmarshal a JSON string into a struct instance */ 13 | 14 | // define a struct into which to unmarshal 15 | type User struct { 16 | FirstName string `json:"firstname"` 17 | LastName string `json:"lastname"` 18 | } 19 | 20 | // initialize a struct 21 | var user User 22 | 23 | // initialize the json string 24 | js := `{"firstname":"John","lastname":"Smith"}` 25 | 26 | // unmarshal the string into the struct 27 | if err := json.Unmarshal([]byte(js), &user); err != nil { 28 | // log.Fatalf("failed to unmarshal json: %v", err) 29 | } 30 | 31 | // fmt.Println(user.FirstName) 32 | -------------------------------------------------------------------------------- /go/loop-for: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - control 5 | - for 6 | - go 7 | - loop 8 | - while 9 | --- 10 | i := 0 11 | for i < 10 { 12 | // ... 13 | i = i+1 14 | } 15 | -------------------------------------------------------------------------------- /go/loop-for-initialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - control 5 | - for 6 | - go 7 | - init 8 | - loop 9 | - while 10 | --- 11 | for i := 0; i < 10; { 12 | // ... 13 | i = i+1 14 | } 15 | -------------------------------------------------------------------------------- /go/loop-range: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - control 5 | - foreach 6 | - go 7 | - loop 8 | --- 9 | for i, val := range []int{ 1, 2, 3 } { 10 | fmt.Println(fmt.Sprintf("%d: %d", i, val)) 11 | } 12 | -------------------------------------------------------------------------------- /go/main-scaffold: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - main 6 | - scaffold 7 | --- 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | ) 13 | 14 | func main() { 15 | fmt.Println("Hello, World!") 16 | } 17 | -------------------------------------------------------------------------------- /go/map: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - map 6 | - structure 7 | --- 8 | m := map[string]int{ 9 | "one" : 1, 10 | "two" : 2, 11 | "three" : 3, 12 | } 13 | -------------------------------------------------------------------------------- /go/map-if-ok: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - contains 5 | - control 6 | - exists 7 | - go 8 | - isset 9 | - map 10 | - ok 11 | --- 12 | m := map[string]int{ 13 | "one" : 1, 14 | "two" : 2, 15 | "three" : 3, 16 | } 17 | 18 | if val, ok := m["one"]; ok { 19 | fmt.Println(val) 20 | } 21 | -------------------------------------------------------------------------------- /go/mutex: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/pattern-decorator: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/pattern-factory: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/pattern-semaphor: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/regex-test: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/select: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-append: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - add 5 | - after 6 | - append 7 | - combine 8 | - go 9 | - join 10 | - push 11 | - slice 12 | --- 13 | orig := []int{1, 2} 14 | appd := append(orig, 3) 15 | -------------------------------------------------------------------------------- /go/slice-capacity: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - capacity 5 | - go 6 | - slice 7 | --- 8 | c = cap([]int{1, 2, 3}) 9 | -------------------------------------------------------------------------------- /go/slice-combine: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - combine 5 | - concat 6 | - go 7 | - merge 8 | - slice 9 | --- 10 | // combine two slices into a single slice 11 | a := []int{1, 2, 3} 12 | b := []int{4, 5, 6} 13 | c := append(a, b...) 14 | -------------------------------------------------------------------------------- /go/slice-element-random: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - capacity 5 | - element 6 | - get 7 | - go 8 | - item 9 | - random 10 | --- 11 | set := []int{1, 2, 3} 12 | item := set[rand.Intn(len(set))] 13 | -------------------------------------------------------------------------------- /go/slice-init: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - create 5 | - go 6 | - initialize 7 | - slice 8 | --- 9 | s := []string{"foo", "bar", "baz"} 10 | -------------------------------------------------------------------------------- /go/slice-length: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - length 6 | - slice 7 | --- 8 | l := len([]int{1, 2, 3}) 9 | -------------------------------------------------------------------------------- /go/slice-pop: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-prepend: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - add 5 | - beginning 6 | - combine 7 | - go 8 | - join 9 | - prepend 10 | - slice 11 | --- 12 | orig := []int{2, 3} 13 | prep := append([]int{1}, orig...) 14 | -------------------------------------------------------------------------------- /go/slice-reverse: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - reverse 6 | - slice 7 | --- 8 | a := []int{1, 2, 3, 4, 5} 9 | 10 | for i := len(a)/2-1; i >= 0; i-- { 11 | opp := len(a)-1-i 12 | a[i], a[opp] = a[opp], a[i] 13 | } 14 | -------------------------------------------------------------------------------- /go/slice-shift: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-shuffle: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - randomize 6 | - shuffle 7 | - slice 8 | --- 9 | // seed the random number generator 10 | // NB: this seed is not cryptographically random. Use the `crypto_rand` package 11 | // if you need cryptographic entropy. 12 | rand.Seed(time.Now().Unix()) 13 | 14 | // shuffle the slice 15 | foo := []int{1, 2, 3, 4, 5} 16 | rand.Shuffle(len(foo), func(i, j int) { 17 | foo[i], foo[j] = foo[j], foo[i] 18 | }) 19 | -------------------------------------------------------------------------------- /go/slice-slice: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-sort: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-spread: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/slice-to-string: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - convert 5 | - go 6 | - implode 7 | - join 8 | - slice 9 | - string 10 | --- 11 | joined := strings.Join([]string{"foo", "bar", "bar"}, " ") 12 | -------------------------------------------------------------------------------- /go/slice-unshift: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/sql-database-connect: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - connect 5 | - database 6 | - db 7 | - go 8 | - open 9 | - sql 10 | --- 11 | // connect to the database 12 | db, err := sql.Open("sqlite3", "/tmp/db.sqlite3") 13 | if err != nil { 14 | // log.Fatalf("failed to connect to database: %v", err) 15 | } 16 | defer db.Close() 17 | -------------------------------------------------------------------------------- /go/sql-exec: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - run 5 | - db 6 | - execute 7 | - go 8 | - query 9 | - sql 10 | --- 11 | if result, err := db.Exec("INSERT INTO foo(bar) VALUES ('baz')"); err != nil { 12 | // log.Fatalf("failed to execute: %v", err) 13 | } 14 | -------------------------------------------------------------------------------- /go/sql-exec-prepared-statement: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/sql-prepare-statement: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - database 5 | - db 6 | - go 7 | - prepare 8 | - sql 9 | - statement 10 | - stmt 11 | --- 12 | stmt, err := db.Prepare("INSERT INTO foo(id, bar) VALUES(?, ?)") 13 | if err != nil { 14 | return fmt.Errof("failed to initialize prepared statement: %v", err) 15 | } 16 | defer stmt.Close() 17 | -------------------------------------------------------------------------------- /go/sql-query: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - db 5 | - go 6 | - query 7 | - select 8 | - sql 9 | --- 10 | // query rows from the database 11 | rows, err := db.Query("SELECT ID, foo, bar FROM baz") 12 | if err != nil { 13 | // log.Fatalf("failed to query database: %v", err) 14 | } 15 | defer rows.Close() 16 | 17 | // iterate over each row 18 | for rows.Next() { 19 | 20 | // package each row into a struct 21 | baz := Baz{} 22 | err = rows.Scan( 23 | &baz.ID, 24 | &baz.foo, 25 | &baz.bar, 26 | ) 27 | 28 | // handle scanning errors 29 | err = rows.Err() 30 | if err != nil { 31 | // log.Fatalf("failed to scan row: %v", err) 32 | } 33 | 34 | // ... now do something with the `baz` struct 35 | } 36 | -------------------------------------------------------------------------------- /go/sql-query-prepared-statement: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/sql-run: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/sql-run-prepared-statement: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/sql-transaction-prepare-statement: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - database 5 | - db 6 | - go 7 | - prepare 8 | - sql 9 | - statement 10 | - stmt 11 | - transaction 12 | - tx 13 | --- 14 | // initialize a transaction 15 | tx, err := db.Begin() 16 | if err != nil { 17 | // log.Fatal(err) 18 | } 19 | // the rollback will be ignored if the later commit succeeds 20 | defer tx.Rollback() 21 | 22 | // initialize a prepared statement 23 | stmt, err := tx.Prepare(` 24 | INSERT INTO foo(id, bar) 25 | VALUES(?, ?)`, 26 | ) 27 | if err != nil { 28 | // log.Fatal(err) 29 | } 30 | defer stmt.Close() 31 | 32 | // loop over records to insert and execute the statement 33 | for id, val := range []string{"foo", "bar", "baz"} { 34 | if _, err := stmt.Exec(id+1, val); err != nil { 35 | // log.Fatal(err) 36 | } 37 | } 38 | 39 | // commit the transaction on successful insertions 40 | if err := tx.Commit(); err != nil { 41 | // log.Fatal(err) 42 | } 43 | -------------------------------------------------------------------------------- /go/sqlx-database-connect: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - connect 5 | - database 6 | - db 7 | - go 8 | - open 9 | - sqlx 10 | --- 11 | // connect to the database 12 | db, err := sqlx.Connect("sqlite3", "/tmp/db.sqlite3") 13 | if err != nil { 14 | // log.Fatalf("failed to connect to database: %v", err) 15 | } 16 | defer db.Close() 17 | -------------------------------------------------------------------------------- /go/sqlx-query-prepared-statement: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - db 5 | - go 6 | - prepared 7 | - query 8 | - select 9 | - sql 10 | - sqlx 11 | - statement 12 | - stmt 13 | --- 14 | // initialize the prepared statement 15 | stmt, err := db.Preparex("SELECT * FROM foo WHERE bar = $1 AND baz = $2") 16 | if err != nil { 17 | // log.Fatalf("failed to prepare statement: %v", err) 18 | } 19 | 20 | // query records into a slice of structs 21 | foos := []Foo{} 22 | if err = stmt.Get(&foos, "qux", "quux"); err != nil { 23 | // log.Fatalf("failed to query database: %v", err) 24 | } 25 | -------------------------------------------------------------------------------- /go/sqlx-select: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - db 5 | - go 6 | - query 7 | - select 8 | - sql 9 | - sqlx 10 | --- 11 | // define a struct to map to table rows 12 | type user struct { 13 | ID int `db:"id"` 14 | Name string `db:"name"` 15 | } 16 | 17 | // initialize a slice to store row data 18 | users := []users{} 19 | 20 | // query the database 21 | if err := db.Select(&users, "SELECT * FROM users"); err != nil { 22 | // log.Fatalf("failed to select users: %v", err) 23 | } 24 | -------------------------------------------------------------------------------- /go/stderr-log: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/stderr-write: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/stdin-arguments-read: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/stdin-read-line: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/stdin-read-loop: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - input 6 | - io 7 | - pipe 8 | - read 9 | - stdin 10 | --- 11 | // read from stdin indefinitely 12 | scanner := bufio.NewScanner(os.Stdin) 13 | for scanner.Scan() { 14 | // fmt.Println(scanner.Text()) 15 | } 16 | 17 | if err := scanner.Err(); err != nil { 18 | // log.Fatalf("failed to read stdin: %v", err) 19 | } 20 | -------------------------------------------------------------------------------- /go/stdout-log: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/stdout-write: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/string-split: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - explode 5 | - go 6 | - split 7 | - string 8 | --- 9 | parts := strings.Split("foo bar baz", " ") 10 | -------------------------------------------------------------------------------- /go/string-to-float: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - coerce 5 | - convert 6 | - float 7 | - go 8 | - string 9 | --- 10 | // second param is float size 11 | f, err := strconv.ParseFloat("3.1415", 64) 12 | if err != nil { 13 | // log.Fatalf("failed to parse string as float: %v", err) 14 | } 15 | -------------------------------------------------------------------------------- /go/string-to-integer: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - atoi 5 | - coerce 6 | - convert 7 | - go 8 | - integer 9 | - parseint 10 | - string 11 | --- 12 | i, err := strconv.Atoi("10") 13 | if err != nil { 14 | // log.Fatalf("failed to convert to integer: %v", err) 15 | } 16 | -------------------------------------------------------------------------------- /go/string-to-lowercase: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - convert 5 | - go 6 | - lowercase 7 | - string 8 | --- 9 | lower := strings.ToLower("FOO") 10 | -------------------------------------------------------------------------------- /go/string-to-readable: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/string-to-time: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - coerce 5 | - convert 6 | - date 7 | - go 8 | - string 9 | - time 10 | - timestamp 11 | --- 12 | // formatting constants: https://golang.org/pkg/time/#pkg-constants 13 | // first param is format string; second param is date to parse 14 | today, err := time.Parse("2006-01-02", "2020-01-01") 15 | if err != nil { 16 | // log.Fatalf("failed to parse date string: %v", err) 17 | } 18 | -------------------------------------------------------------------------------- /go/string-to-uppercase: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - convert 5 | - go 6 | - uppercase 7 | - string 8 | --- 9 | upper := strings.ToUpper("foo") 10 | -------------------------------------------------------------------------------- /go/string-trim: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - string 6 | - trim 7 | - whitespace 8 | --- 9 | trimmed := strings.TrimSpace(" foo\n") 10 | -------------------------------------------------------------------------------- /go/struct: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - struct 6 | - type 7 | --- 8 | // Foo represents... 9 | type Foo struct { 10 | ID int `json:"id,string"` 11 | Bar string `json:"bar,string"` 12 | } 13 | -------------------------------------------------------------------------------- /go/struct-anonymous: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - anonymous 5 | - go 6 | - struct 7 | --- 8 | user := struct { 9 | ID int 10 | First string 11 | Last string 12 | }{ 13 | 1, 14 | "John", 15 | "Smith", 16 | } 17 | -------------------------------------------------------------------------------- /go/struct-anonymous-slice: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - anonymous 5 | - go 6 | - slice 7 | - struct 8 | --- 9 | users := []struct { 10 | ID int 11 | First string 12 | Last string 13 | }{ 14 | {1, "John", "Smith"}, 15 | {2, "Jane", "Smith"}, 16 | } 17 | -------------------------------------------------------------------------------- /go/struct-method: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - class 5 | - function 6 | - go 7 | - method 8 | - struct 9 | --- 10 | // Baz does... 11 | func (f *Foo) Baz() string { 12 | return "baz" 13 | } 14 | -------------------------------------------------------------------------------- /go/struct-scaffold: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - class 5 | - go 6 | - scaffold 7 | - struct 8 | - type 9 | --- 10 | package foo 11 | 12 | import ( 13 | // ... 14 | ) 15 | 16 | // Foo represents... 17 | type Foo struct { 18 | ID int `json:"id,string"` 19 | Bar string `json:"bar,string"` 20 | } 21 | 22 | // New returns a new Foo struct 23 | func New(id int, bar string) (Foo, error) { 24 | return Foo{ 25 | ID: id, 26 | Bar: bar, 27 | }, nil 28 | } 29 | 30 | // Baz does... 31 | func (f *Foo) Baz() string { 32 | return "baz" 33 | } 34 | -------------------------------------------------------------------------------- /go/switch: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - case 5 | - conditional 6 | - control 7 | - go 8 | - switch 9 | --- 10 | switch day { 11 | case "sunday": 12 | // cases don't "fall through" by default! 13 | fallthrough 14 | 15 | case "saturday": 16 | rest() 17 | 18 | default: 19 | work() 20 | } 21 | -------------------------------------------------------------------------------- /go/switch-no-condition: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - case 5 | - chain 6 | - conditional 7 | - control 8 | - go 9 | - switch 10 | - true 11 | --- 12 | // switch without a condition is the same as switch true. This can be a clean 13 | // way to write long if-then-else chains. 14 | t := time.Now() 15 | switch { 16 | case t.Hour() < 12: 17 | fmt.Println("Good morning!") 18 | case t.Hour() < 17: 19 | fmt.Println("Good afternoon.") 20 | default: 21 | fmt.Println("Good evening.") 22 | } 23 | -------------------------------------------------------------------------------- /go/system-execute: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - execute 5 | - go 6 | - shell 7 | - syscall 8 | - thread 9 | --- 10 | TODO 11 | -------------------------------------------------------------------------------- /go/test-bench: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/test-function: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - function 5 | - go 6 | - scaffold 7 | - testing 8 | --- 9 | // TestFoo asserts that... 10 | func TestFoo(t *testing.T) { 11 | good := "good" 12 | bad := "bad" 13 | if good != bad { 14 | t.Errorf("failed to function: want: %s, got: %s", good, bad) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /go/test-scaffold: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - scaffold 6 | - testing 7 | --- 8 | package foo 9 | 10 | import ( 11 | "testing" 12 | 13 | //"github.com/MYORG/MYPKG/internal/FOO" 14 | ) 15 | 16 | // TestFoo asserts that... 17 | func TestFoo(t *testing.T) { 18 | good := "good" 19 | bad := "bad" 20 | if good != bad { 21 | t.Errorf("failed to function: want: %s, got: %s", good, bad) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /go/time-add-subtract: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - date 5 | - day 6 | - from 7 | - go 8 | - month 9 | - next 10 | - previous 11 | - since 12 | - time 13 | - timestamp 14 | - tomorrow 15 | - year 16 | - yesterday 17 | --- 18 | // get timestamp 1 day from now (in the future) 19 | tomorrow := time.Now().AddDate(0, 0, 1) 20 | -------------------------------------------------------------------------------- /go/time-get-year-month-day: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - date 5 | - day 6 | - go 7 | - month 8 | - time 9 | - timestamp 10 | - year 11 | --- 12 | /* Get year, month, day of time */ 13 | 14 | // all: 15 | year, month, day := time.Now().Date() 16 | 17 | // individual: 18 | year := time.Now().Year() // type int 19 | month := time.Now().Month() // type time.Month 20 | day := time.Now().Day() // type int 21 | -------------------------------------------------------------------------------- /go/timestamp-create: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - date 5 | - go 6 | - time 7 | - timestamp 8 | --- 9 | // create a timestmap (type: Time) 10 | ts := time.Now() 11 | -------------------------------------------------------------------------------- /go/timestamp-create-unix: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - date 5 | - go 6 | - time 7 | - timestamp 8 | - unix 9 | --- 10 | // create a Unix timestmap (type: int64) 11 | ts := time.Now().Unix() 12 | -------------------------------------------------------------------------------- /go/timestamp-format: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - date 5 | - go 6 | - time 7 | - timestamp 8 | - format 9 | --- 10 | // format today's date as "YYYY-MM-DD" (type: string) 11 | ts := time.Now().Format("2006-01-02") 12 | // fmt.Println(ts) 13 | -------------------------------------------------------------------------------- /go/type-assert: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - assert 5 | - coerce 6 | - convert 7 | - go 8 | - type 9 | --- 10 | // assert that an empty interface is a string 11 | var i interface{} = "hello" 12 | s := i.(string) 13 | -------------------------------------------------------------------------------- /go/url-parse: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/var-declare: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - variable 6 | - declare 7 | --- 8 | var pi float64 9 | -------------------------------------------------------------------------------- /go/var-initialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - go 5 | - variable 6 | - initialize 7 | --- 8 | pi := 3.14 9 | // var pi float64 = 3.14 10 | -------------------------------------------------------------------------------- /go/waitgroup: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /go/yaml-marshal: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - encode 5 | - go 6 | - marshal 7 | - serialize 8 | - struct 9 | - yaml 10 | --- 11 | /* Marshal a struct into a YAML string */ 12 | 13 | // define a struct to marshal 14 | type User struct { 15 | FirstName string `yaml:"firstname"` 16 | LastName string `yaml:"lastname"` 17 | } 18 | 19 | // initialze a struct 20 | user := User{ 21 | FirstName: "John", 22 | LastName: "Smith", 23 | } 24 | 25 | // marshal the struct into a slice of bytes 26 | bs, err := yaml.Marshal(&user) 27 | if err != nil { 28 | // log.Fatalf("failed to marshal yaml: %v", err) 29 | } 30 | 31 | // coerce the slice of bytes to a string 32 | // fmt.Println(string(bs)) 33 | -------------------------------------------------------------------------------- /go/yaml-unmarshal: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: go 3 | tags: 4 | - decode 5 | - go 6 | - parse 7 | - struct 8 | - unmarshal 9 | - unserialize 10 | - yaml 11 | --- 12 | /* Unmarshal a YAML string into a struct instance */ 13 | 14 | // define a struct into which to unmarshal 15 | type User struct { 16 | FirstName string `yaml:"firstname"` 17 | LastName string `yaml:"lastname"` 18 | } 19 | 20 | // initialize a struct 21 | var user User 22 | 23 | // initialize the yaml string 24 | yml := "---\nfirstname: John\nlastname: Smith" 25 | 26 | // unmarshal the string into the struct 27 | if err := yaml.Unmarshal([]byte(yml), &user); err != nil { 28 | // log.Fatalf("failed to unmarshal yaml: %v", err) 29 | } 30 | 31 | // fmt.Println(user.FirstName) 32 | -------------------------------------------------------------------------------- /js/array-append: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - add 5 | - after 6 | - append 7 | - array 8 | - insert 9 | - javascript 10 | - push 11 | --- 12 | // append `baz` to the array 13 | let arr = ['foo', 'bar']; 14 | arr.push('baz'); 15 | -------------------------------------------------------------------------------- /js/array-concat: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - add 5 | - array 6 | - combine 7 | - concatenate 8 | - javascript 9 | - merge 10 | --- 11 | let arr1 = ['foo', 'bar']; 12 | let arr2 = ['baz', 'qux']; 13 | let union = arr1.concat(arr2); 14 | -------------------------------------------------------------------------------- /js/array-every: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - check 5 | - every 6 | - javascript 7 | - object 8 | - test 9 | --- 10 | // `allEven` will be `true` if every element in `arr` is even 11 | let arr = [2, 4, 6, 8]; 12 | let allEven = arr.every(n => { 13 | return n % 2 == 0; 14 | }); 15 | -------------------------------------------------------------------------------- /js/array-filter: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - javascript 6 | - filter 7 | --- 8 | // filter `arr` to contain only even numbers 9 | let arr = [1, 2, 3, 4, 5, 6]; 10 | let evens = arr.filter(item => { 11 | return item % 2 == 0; 12 | }); 13 | -------------------------------------------------------------------------------- /js/array-includes: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - contains 6 | - includes 7 | - javascript 8 | --- 9 | let arr = ['foo', 'bar', 'baz']; 10 | // NB: `includes` is case-sensitive 11 | let inc = arr.includes('foo'); 12 | -------------------------------------------------------------------------------- /js/array-initialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - create 6 | - initialize 7 | - javascript 8 | - new 9 | --- 10 | let arr = ['foo', 'bar', 'baz']; 11 | -------------------------------------------------------------------------------- /js/array-length: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - count 6 | - javascript 7 | - length 8 | --- 9 | let arr = ['foo', 'bar', 'baz']; 10 | let length = arr.length; 11 | -------------------------------------------------------------------------------- /js/array-map: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - iterate 6 | - javascript 7 | - loop 8 | - map 9 | --- 10 | let arr = [1, 2, 3, 4, 5]; 11 | let doubled = arr.map(n => { 12 | return n * 2; 13 | }); 14 | -------------------------------------------------------------------------------- /js/array-pop: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - end 6 | - javascript 7 | - pop 8 | - remove 9 | --- 10 | let arr = ['foo', 'bar', 'baz']; 11 | let baz = arr.pop(); 12 | -------------------------------------------------------------------------------- /js/array-prepend: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - add 5 | - array 6 | - before 7 | - insert 8 | - javascript 9 | - preprend 10 | - unshift 11 | --- 12 | // prepend `foo` to the array 13 | let arr = ['bar', 'baz']; 14 | arr.unshift('foo'); 15 | -------------------------------------------------------------------------------- /js/array-push: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - append 5 | - array 6 | - insert 7 | - javascript 8 | - push 9 | --- 10 | let arr = ['foo', 'bar']; 11 | arr.push('baz'); 12 | -------------------------------------------------------------------------------- /js/array-reverse: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - javascript 6 | - reverse 7 | --- 8 | let arr = ['foo', 'bar', 'baz']; 9 | arr.reverse(); 10 | -------------------------------------------------------------------------------- /js/array-shift: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - before 6 | - first 7 | - javascript 8 | - remove 9 | - shift 10 | --- 11 | // remove `foo` from the array 12 | let arr = ['foo', 'bar', 'baz']; 13 | let foo = arr.shift(); 14 | -------------------------------------------------------------------------------- /js/array-some: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - check 5 | - javascript 6 | - object 7 | - some 8 | - test 9 | --- 10 | // `someEven` will be `true` if some elements in `arr` are even 11 | let arr = [2, 4, 6, 8]; 12 | let someEven = arr.some(n => { 13 | return n % 2 == 0; 14 | }); 15 | -------------------------------------------------------------------------------- /js/array-sort: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - javascript 6 | - sort 7 | --- 8 | let arr = ['foo', 'bar', 'baz']; 9 | arr.sort(); 10 | -------------------------------------------------------------------------------- /js/array-to-string: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - convert 6 | - implode 7 | - javascript 8 | - join 9 | - string 10 | --- 11 | let arr = ['foo', 'bar', 'baz']; 12 | let str = arr.toString(); 13 | // str: 'foo,bar,baz' 14 | -------------------------------------------------------------------------------- /js/array-to-string-join: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - convert 6 | - implode 7 | - javascript 8 | - join 9 | - string 10 | --- 11 | let arr = ['foo', 'bar', 'baz']; 12 | // NB: param is optional separator, and defaults to `,` 13 | let str = arr.join('\n'); 14 | // str: 'foo\nbar\nbaz' 15 | -------------------------------------------------------------------------------- /js/function: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - function 5 | - javascript 6 | --- 7 | function foo(bar) { 8 | return bar; 9 | } 10 | -------------------------------------------------------------------------------- /js/function-closure: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - anonymous 5 | - closure 6 | - function 7 | - javascript 8 | - lambda 9 | --- 10 | (function(str) { 11 | console.log(str) 12 | })('foo') 13 | -------------------------------------------------------------------------------- /js/function-memoize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - cache 5 | - function 6 | - javascript 7 | - memoize 8 | --- 9 | function foo(bar) { 10 | // if the result has been memoized, return it and exit early 11 | if (this.memo) { 12 | return this.memo; 13 | } 14 | 15 | // otherwise, compute, memoize, and return the result 16 | this.memo = 1; 17 | return this.memo; 18 | } 19 | -------------------------------------------------------------------------------- /js/if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - conditional 5 | - control 6 | - javascript 7 | --- 8 | if (true) { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /js/if-else: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - conditional 5 | - control 6 | - javascript 7 | --- 8 | if (true) { 9 | 10 | } else { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /js/if-else-if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - conditional 5 | - control 6 | - javascript 7 | --- 8 | if (true) { 9 | 10 | } else if (true) { 11 | 12 | } else { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /js/json-serialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - javascript 6 | - json 7 | - object 8 | - serialize 9 | - stringify 10 | --- 11 | let user = { first: 'John', last: 'Doe' }; 12 | let json = JSON.stringify(user); 13 | -------------------------------------------------------------------------------- /js/json-unserialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - javascript 6 | - json 7 | - object 8 | - parse 9 | - unserialize 10 | - unstringify 11 | --- 12 | let json = '{"first":"John","last":"Doe"}'; 13 | let user = JSON.parse(json); 14 | -------------------------------------------------------------------------------- /js/loop-do-while: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - control 5 | - do 6 | - javascript 7 | - loop 8 | - while 9 | --- 10 | let i = 0; 11 | do { 12 | // ... 13 | i++; 14 | } 15 | while (i < 10); 16 | -------------------------------------------------------------------------------- /js/loop-for: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - control 5 | - for 6 | - javascript 7 | - loop 8 | --- 9 | for (i = 0; i < 10; i++) { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /js/loop-for-of: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - control 6 | - for 7 | - iterate 8 | - javascript 9 | - loop 10 | - map 11 | - object 12 | - set 13 | - string 14 | --- 15 | // unlike `forEach`, `for of` can iterate over any iterable 16 | let arr = ['foo', 'bar', 'baz']; 17 | // NB: `let` in loop may be one of: `const`, `let`, `var` 18 | for (let x of arr) { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /js/loop-foreach: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - control 6 | - iterate 7 | - javascript 8 | - loop 9 | --- 10 | ['foo', 'bar', 'baz'].forEach(item => { 11 | console.log(item); 12 | }); 13 | -------------------------------------------------------------------------------- /js/loop-while: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - control 5 | - javascript 6 | - loop 7 | - while 8 | --- 9 | let i = 0; 10 | while (i < 10) { 11 | // ... 12 | i++; 13 | } 14 | -------------------------------------------------------------------------------- /js/number-absolute-value: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - absolute 5 | - float 6 | - int 7 | - javascript 8 | - math 9 | - number 10 | - value 11 | --- 12 | let val = Math.abs(-10); 13 | -------------------------------------------------------------------------------- /js/number-exponentation: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - exponentation 5 | - float 6 | - int 7 | - javascript 8 | - math 9 | - number 10 | - power 11 | --- 12 | let num = Math.pow(10, 2); 13 | -------------------------------------------------------------------------------- /js/number-is-a-number: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - is 5 | - javascript 6 | - math 7 | - number 8 | --- 9 | let isNum = !isNaN(123); 10 | -------------------------------------------------------------------------------- /js/number-is-not-a-number: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - is 5 | - javascript 6 | - math 7 | - not 8 | - number 9 | --- 10 | let nan = isNaN(123); 11 | -------------------------------------------------------------------------------- /js/number-maximum: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - float 5 | - int 6 | - javascript 7 | - math 8 | - maximum 9 | - number 10 | --- 11 | let arr = [1, 2, 3]; 12 | let max = Math.max(...arr); 13 | -------------------------------------------------------------------------------- /js/number-minimum: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - float 5 | - int 6 | - javascript 7 | - math 8 | - minimum 9 | - number 10 | --- 11 | let arr = [1, 2, 3]; 12 | let min = Math.min(...arr); 13 | -------------------------------------------------------------------------------- /js/number-random: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - float 5 | - int 6 | - javascript 7 | - math 8 | - number 9 | - random 10 | --- 11 | // generate a random number between 0-1 (exclusive) 12 | let num = Math.random(); 13 | 14 | // generate a random number between 0-100 (inclusive) 15 | let num = Math.floor((Math.random() * 100) + 1); 16 | -------------------------------------------------------------------------------- /js/number-round: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - float 5 | - int 6 | - javascript 7 | - math 8 | - number 9 | - round 10 | --- 11 | let num = Math.round(9.5); 12 | -------------------------------------------------------------------------------- /js/number-round-down: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - down 5 | - float 6 | - floor 7 | - int 8 | - javascript 9 | - math 10 | - number 11 | - round 12 | --- 13 | let num = Math.floor(9.5); 14 | -------------------------------------------------------------------------------- /js/number-round-up: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - ceil 5 | - float 6 | - int 7 | - javascript 8 | - math 9 | - number 10 | - round 11 | - up 12 | --- 13 | let num = Math.ceil(9.5); 14 | -------------------------------------------------------------------------------- /js/object-initialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - create 5 | - initialize 6 | - javascript 7 | - object 8 | --- 9 | let user = { 10 | first: 'John', 11 | last: 'Doe', 12 | }; 13 | -------------------------------------------------------------------------------- /js/object-keys: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - get 5 | - javascript 6 | - keys 7 | - object 8 | --- 9 | let user = { first: 'John', last: 'Doe' }; 10 | let keys = Object.keys(user); 11 | -------------------------------------------------------------------------------- /js/string-includes: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - contains 5 | - includes 6 | - javascript 7 | - string 8 | - substring 9 | --- 10 | let str = 'hello, world!'; 11 | let inc = str.includes('world'); 12 | -------------------------------------------------------------------------------- /js/string-interpolate: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - interpolate 5 | - javascript 6 | - string 7 | --- 8 | let color = 'brown'; 9 | console.log(`The quick ${color} fox jumped over the lazy dog.`); 10 | -------------------------------------------------------------------------------- /js/string-length: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - count 5 | - javascript 6 | - length 7 | - string 8 | --- 9 | let length = 'Hello, world!'.length; 10 | -------------------------------------------------------------------------------- /js/string-replace: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - javascript 5 | - replace 6 | - string 7 | - substring 8 | --- 9 | let hello = 'Hello, world!'; 10 | let goodbye = hello.replace('Hello', 'Goodbye'); 11 | -------------------------------------------------------------------------------- /js/string-reverse: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - javascript 5 | - reverse 6 | - string 7 | --- 8 | let str = 'Hello, world!'; 9 | let rev = str.split('').reverse().join(''); 10 | -------------------------------------------------------------------------------- /js/string-substring: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - javascript 5 | - string 6 | - substring 7 | --- 8 | let str = 'Hello, world!'.substr(0, 5); // 'Hello' 9 | -------------------------------------------------------------------------------- /js/string-to-array: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - array 5 | - convert 6 | - explode 7 | - javascript 8 | - split 9 | - string 10 | - tokenize 11 | --- 12 | let arr = 'Hello, world!'.split(' '); 13 | -------------------------------------------------------------------------------- /js/string-to-float: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - float 6 | - javascript 7 | - number 8 | - parse 9 | - string 10 | --- 11 | let num = parseFloat("1.0"); 12 | -------------------------------------------------------------------------------- /js/string-to-int: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - int 6 | - javascript 7 | - number 8 | - parse 9 | - string 10 | --- 11 | let num = parseInt("1"); 12 | -------------------------------------------------------------------------------- /js/string-to-lowercase: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - javascript 6 | - lowercase 7 | - string 8 | --- 9 | let str = 'HELLO, WORLD'.toLowerCase(); 10 | -------------------------------------------------------------------------------- /js/string-to-uppercase: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - convert 5 | - javascript 6 | - string 7 | - uppercase 8 | --- 9 | let str = 'hello, world'.toUpperCase(); 10 | -------------------------------------------------------------------------------- /js/string-trim: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - javascript 5 | - string 6 | - trim 7 | - whitespace 8 | --- 9 | let str = " foo\n"; 10 | let trimmed = str.trim(); 11 | -------------------------------------------------------------------------------- /js/switch: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - case 5 | - conditional 6 | - control 7 | - javascript 8 | - switch 9 | --- 10 | switch(value) { 11 | case 1: 12 | // handle 1 13 | break; 14 | case 2: 15 | case 3: 16 | // handle 2 and 3 17 | break; 18 | case 4: 19 | // handle 4 20 | break; 21 | default: 22 | // handle all other values 23 | } 24 | -------------------------------------------------------------------------------- /js/ternary: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - conditional 5 | - control 6 | - javascript 7 | --- 8 | let condition = true 9 | let val = condition ? 'yes' : 'no'; 10 | -------------------------------------------------------------------------------- /js/throw: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - error 5 | - javascript 6 | - throw 7 | --- 8 | throw "failed to reticulate splines"; 9 | -------------------------------------------------------------------------------- /js/try-catch: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - catch 5 | - error 6 | - handling 7 | - javascript 8 | - try 9 | --- 10 | try { 11 | 12 | } catch(err) { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /js/try-catch-finally: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - catch 5 | - error 6 | - finally 7 | - handling 8 | - javascript 9 | - try 10 | --- 11 | try { 12 | 13 | } catch(err) { 14 | 15 | } finally { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /lua/function: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - function 5 | - lua 6 | --- 7 | -- functions may return multiple values 8 | function foo(bar, baz) 9 | return bar, baz 10 | end 11 | -------------------------------------------------------------------------------- /lua/function-closure: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - anonymous 5 | - closure 6 | - function 7 | - lua 8 | --- 9 | (function(foo) 10 | return foo 11 | end)() 12 | -------------------------------------------------------------------------------- /lua/function-varargs: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /lua/if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - conditional 5 | - control 6 | - lua 7 | --- 8 | if true then 9 | 10 | end 11 | -------------------------------------------------------------------------------- /lua/if-else: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - conditional 5 | - control 6 | - lua 7 | --- 8 | if true then 9 | 10 | else 11 | 12 | end 13 | -------------------------------------------------------------------------------- /lua/if-else-if: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - conditional 5 | - control 6 | - lua 7 | --- 8 | if true then 9 | 10 | elseif true then 11 | 12 | else 13 | 14 | end 15 | -------------------------------------------------------------------------------- /lua/loop-for: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - control 5 | - for 6 | - loop 7 | - lua 8 | --- 9 | -- vars are from, to, and step value 10 | for i = 1, 10, 1 do 11 | print(i) 12 | end 13 | -------------------------------------------------------------------------------- /lua/loop-ipairs: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - control 5 | - foreach 6 | - iterate 7 | - loop 8 | - lua 9 | - table 10 | --- 11 | -- iterate over numerically-indexed table 12 | for i, val in ipairs({"foo", "bar", "baz"}) do 13 | -- print(i .. ":" .. val) 14 | end 15 | -------------------------------------------------------------------------------- /lua/loop-pairs: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - control 5 | - foreach 6 | - iterate 7 | - loop 8 | - lua 9 | - table 10 | --- 11 | -- iterate over a table's properties 12 | local user = {first = "Linus", last = "Torvalds"} 13 | for key, val in pairs(user) do 14 | -- print(key .. ":" .. val) 15 | end 16 | -------------------------------------------------------------------------------- /lua/loop-repeat: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - control 5 | - iterate 6 | - loop 7 | - lua 8 | - repeat 9 | - until 10 | --- 11 | repeat 12 | 13 | until false 14 | -------------------------------------------------------------------------------- /lua/loop-while: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - control 5 | - loop 6 | - lua 7 | - while 8 | --- 9 | while true do 10 | 11 | end 12 | -------------------------------------------------------------------------------- /lua/string-length: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - lua 5 | - string 6 | --- 7 | local len = #"foo" 8 | -------------------------------------------------------------------------------- /lua/switch: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - conditional 5 | - control 6 | - lua 7 | - switch 8 | --- 9 | -- Lua has no `switch` statement. Use the following instead: 10 | if true then 11 | 12 | elseif true then 13 | 14 | else 15 | 16 | end 17 | -------------------------------------------------------------------------------- /lua/table-append: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - add 5 | - array 6 | - insert 7 | - lua 8 | - table 9 | --- 10 | -- append "foo" to the end of table `t` 11 | table.insert(t, "foo") 12 | -------------------------------------------------------------------------------- /lua/table-array-init: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - array 5 | - init 6 | - lua 7 | - table 8 | --- 9 | -- initialize a numerically-indexed table 10 | local t = {"foo", "bar", "baz"} 11 | -------------------------------------------------------------------------------- /lua/table-constructor: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - class 5 | - constructor 6 | - init 7 | - lua 8 | - object 9 | - struct 10 | - table 11 | --- 12 | -- initialize the class metatable 13 | User = {} 14 | 15 | -- define the constructor 16 | function User:new(u) 17 | u = u or {} 18 | setmetatable(u, self) 19 | self.__index = self 20 | return u 21 | end 22 | -------------------------------------------------------------------------------- /lua/table-insert: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - add 5 | - array 6 | - lua 7 | - table 8 | --- 9 | -- insert "foo" into position `3` of table `t` 10 | table.insert(t, 3, "foo") 11 | -------------------------------------------------------------------------------- /lua/table-length: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - length 5 | - lua 6 | - table 7 | --- 8 | local len = #{"foo", "bar", "baz"} 9 | -------------------------------------------------------------------------------- /lua/table-method: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - function 5 | - lua 6 | - method 7 | - table 8 | --- 9 | function Foo:bar(baz) 10 | 11 | end 12 | -------------------------------------------------------------------------------- /lua/table-object-init: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - array 5 | - associative 6 | - init 7 | - lua 8 | - map 9 | - object 10 | - record 11 | - struct 12 | - table 13 | --- 14 | -- initialize a table that functions as an object/map/associative array in 15 | -- other languages 16 | local t = { 17 | foo = "bar", 18 | baz = "bat", 19 | } 20 | -------------------------------------------------------------------------------- /lua/table-prepend: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - add 5 | - array 6 | - lua 7 | - table 8 | --- 9 | -- insert "foo" into position `1` of table `t` 10 | table.insert(t, 3, "foo") 11 | -------------------------------------------------------------------------------- /lua/table-remove: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - array 5 | - delete 6 | - lua 7 | - pop 8 | - table 9 | --- 10 | -- remove (and return) the element at position `3` from table `t`. 11 | -- (`t` will be resized.) 12 | local elem = table.remove(t, 3) 13 | -------------------------------------------------------------------------------- /lua/ternary: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: lua 3 | tags: 4 | - conditional 5 | - control 6 | - lua 7 | --- 8 | local value = true and "yes" or "no" 9 | -------------------------------------------------------------------------------- /markdown/code-block: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - block 5 | - code 6 | - markdown 7 | --- 8 | ```javascript 9 | console.log('hello, world!'); 10 | ``` 11 | -------------------------------------------------------------------------------- /markdown/code-inline: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - inline 5 | - code 6 | - markdown 7 | --- 8 | `console.log('hello, world!');` 9 | -------------------------------------------------------------------------------- /markdown/heading-h1: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h1 5 | - heading 6 | - markdown 7 | --- 8 | # Heading # 9 | -------------------------------------------------------------------------------- /markdown/heading-h2: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h2 5 | - heading 6 | - markdown 7 | --- 8 | ## Subheading ## 9 | -------------------------------------------------------------------------------- /markdown/heading-h3: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h3 5 | - heading 6 | - markdown 7 | --- 8 | ### Subheading ### 9 | -------------------------------------------------------------------------------- /markdown/heading-h4: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h4 5 | - heading 6 | - markdown 7 | --- 8 | #### Subheading #### 9 | -------------------------------------------------------------------------------- /markdown/heading-h5: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h5 5 | - heading 6 | - markdown 7 | --- 8 | ##### Subheading ##### 9 | -------------------------------------------------------------------------------- /markdown/heading-h6: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - h6 5 | - heading 6 | - markdown 7 | --- 8 | ###### Subheading ###### 9 | -------------------------------------------------------------------------------- /markdown/horizontal-line: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - horizontal 5 | - line 6 | - markdown 7 | - rule 8 | --- 9 | ___ 10 | -------------------------------------------------------------------------------- /markdown/image: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - image 5 | - img 6 | - markdown 7 | --- 8 | ![alt text](/images/example.png "title text") 9 | -------------------------------------------------------------------------------- /markdown/image-link: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - hyperlink 5 | - identifier 6 | - image 7 | - img 8 | - markdown 9 | - reference 10 | --- 11 | 12 | [![alt text](/images/example.png "title text")*caption text*][url] 13 | 14 | [url]: https://example.com 15 | -------------------------------------------------------------------------------- /markdown/image-reference: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - identifier 5 | - image 6 | - img 7 | - markdown 8 | - reference 9 | --- 10 | ![alt text][img] 11 | 12 | [img]: /images/example.png 13 | -------------------------------------------------------------------------------- /markdown/link-inline: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - anchor 5 | - hyperlink 6 | - markdown 7 | --- 8 | [Go to Google](https://google.com) 9 | -------------------------------------------------------------------------------- /markdown/link-mailto: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - anchor 5 | - email 6 | - hyperlink 7 | - markdown 8 | --- 9 | [email John Doe](mailto:johndoe@example.com) 10 | -------------------------------------------------------------------------------- /markdown/link-reference: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - anchor 5 | - hyperlink 6 | - markdown 7 | - reference 8 | --- 9 | [Go to Google][google] 10 | 11 | [google]: https://google.com 12 | -------------------------------------------------------------------------------- /markdown/list-ordered: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - list 5 | - markdown 6 | - numbered 7 | - ordered 8 | - text 9 | --- 10 | 1. foo 11 | 2. bar 12 | 3. baz 13 | -------------------------------------------------------------------------------- /markdown/list-unordered: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - list 5 | - markdown 6 | - text 7 | - unordered 8 | --- 9 | - foo 10 | - bar 11 | - baz 12 | -------------------------------------------------------------------------------- /markdown/quote-block: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - blockquote 5 | - markdown 6 | - text 7 | --- 8 | > Lorem ipsum dolor sit amet. 9 | -------------------------------------------------------------------------------- /markdown/quote-block-nested: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - blockquote 5 | - markdown 6 | - text 7 | --- 8 | > Lorem ipsum dolor sit amet. 9 | > 10 | >> Consectetur adipiscing elit. 11 | -------------------------------------------------------------------------------- /markdown/table: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - cell 5 | - column 6 | - data 7 | - markdown 8 | - row 9 | - table 10 | - text 11 | --- 12 | Foo | Bar | Baz 13 | -----|------|----- 14 | data | data | data 15 | data | data | data 16 | data | data | data 17 | -------------------------------------------------------------------------------- /markdown/text-bold: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - bold 5 | - markdown 6 | - text 7 | --- 8 | **bold** 9 | -------------------------------------------------------------------------------- /markdown/text-italic: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - italic 5 | - markdown 6 | - text 7 | --- 8 | __italic__ 9 | -------------------------------------------------------------------------------- /markdown/text-strikethrough: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: markdown 3 | tags: 4 | - delete 5 | - markdown 6 | - strikethrough 7 | - text 8 | --- 9 | ~~strikethrough~~ 10 | -------------------------------------------------------------------------------- /nodejs/csv-read: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/csv-read -------------------------------------------------------------------------------- /nodejs/csv-write: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/csv-write -------------------------------------------------------------------------------- /nodejs/directory-list: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - directory 5 | - filesystem 6 | - get 7 | - javascript 8 | - list 9 | - nodejs 10 | - read 11 | - show 12 | --- 13 | fs.readdir("/path/to/dir", (err, files) => { 14 | if (err) { 15 | // TODO: handle errors 16 | } 17 | 18 | // console.log(files); 19 | }); 20 | -------------------------------------------------------------------------------- /nodejs/directory-list-sync: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - directory 5 | - filesystem 6 | - get 7 | - javascript 8 | - list 9 | - nodejs 10 | - read 11 | - show 12 | - sync 13 | --- 14 | try { 15 | let files = fs.readdirSync("/path/to/dir"); 16 | 17 | // ... 18 | 19 | } catch (err) { 20 | // TODO: handle errors 21 | } 22 | -------------------------------------------------------------------------------- /nodejs/directory-walk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/directory-walk -------------------------------------------------------------------------------- /nodejs/file-read: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/file-read -------------------------------------------------------------------------------- /nodejs/file-read-sync: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - filesystem 5 | - javascript 6 | - nodejs 7 | - read 8 | - sync 9 | --- 10 | try { 11 | // NB: remove the `encoding` option to return `data` as a `Buffer` 12 | let data = fs.readFileSync('/path/to/file', { encoding: 'utf-8'}); 13 | // console.log(data); 14 | } catch (e) { 15 | // TODO: handle errors 16 | } 17 | -------------------------------------------------------------------------------- /nodejs/file-write: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/file-write -------------------------------------------------------------------------------- /nodejs/file-write-sync: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/file-write-sync -------------------------------------------------------------------------------- /nodejs/http-get: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/http-get -------------------------------------------------------------------------------- /nodejs/http-get-json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/http-get-json -------------------------------------------------------------------------------- /nodejs/http-post: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/http-post -------------------------------------------------------------------------------- /nodejs/http-post-json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/http-post-json -------------------------------------------------------------------------------- /nodejs/import: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/import -------------------------------------------------------------------------------- /nodejs/process-exit: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: javascript 3 | tags: 4 | - end 5 | - exit 6 | - javascript 7 | - nodejs 8 | - process 9 | - program 10 | --- 11 | process.exit(0); 12 | -------------------------------------------------------------------------------- /nodejs/require: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/require -------------------------------------------------------------------------------- /nodejs/stderr-log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stderr-log -------------------------------------------------------------------------------- /nodejs/stderr-write: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stderr-write -------------------------------------------------------------------------------- /nodejs/stdin-read-bytes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdin-read-bytes -------------------------------------------------------------------------------- /nodejs/stdin-read-line: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdin-read-line -------------------------------------------------------------------------------- /nodejs/stdin-read-loop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdin-read-loop -------------------------------------------------------------------------------- /nodejs/stdin-read-string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdin-read-string -------------------------------------------------------------------------------- /nodejs/stdout-log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdout-log -------------------------------------------------------------------------------- /nodejs/stdout-write: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheat/snippets/7095275e2b36e181930a363c83d2a67f19b4b7df/nodejs/stdout-write -------------------------------------------------------------------------------- /php/array-concat: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-join: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-key-exists: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-keys: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - keys 6 | - php 7 | - slice 8 | --- 9 | $keys = array_keys(['a', 'b', 'c']); 10 | -------------------------------------------------------------------------------- /php/array-map: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - map 6 | - php 7 | - slice 8 | --- 9 | $cube = function ($n) { 10 | return ($n * $n * $n); 11 | }; 12 | $mapped = array_map($cube, [1, 2, 3]); 13 | -------------------------------------------------------------------------------- /php/array-pop: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-push: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - add 5 | - array 6 | - php 7 | - push 8 | - slice 9 | --- 10 | $array[] = $element; 11 | -------------------------------------------------------------------------------- /php/array-reverse: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - php 6 | - reverse 7 | - slice 8 | --- 9 | $rev = array_reverse(["foo", "bar"]); 10 | -------------------------------------------------------------------------------- /php/array-shift: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - keys 6 | - php 7 | - slice 8 | --- 9 | $letters = ['a', 'b', 'c']; 10 | $a = array_shift($letters); 11 | // $a now equals 'a' 12 | // $letters now equals ['b', 'c'] 13 | -------------------------------------------------------------------------------- /php/array-slice: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-sort: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - php 6 | - slice 7 | - sort 8 | --- 9 | $arr = ['c', 'b', 'a']; 10 | sort($arr); 11 | -------------------------------------------------------------------------------- /php/array-spread: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-to-object: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - array 5 | - coerce 6 | - object 7 | - php 8 | - struct 9 | --- 10 | // coerce an associative array into an Stdclass object 11 | $obj = (object)['foo' => 'bar']; 12 | -------------------------------------------------------------------------------- /php/array-unshift: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/array-values: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/class-function: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/class-scaffold: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/directory-list: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - directory 5 | - list 6 | - php 7 | --- 8 | $files = scandir($dir); 9 | -------------------------------------------------------------------------------- /php/directory-walk: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/file-read: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - file 5 | - filesystem 6 | - io 7 | - php 8 | - read 9 | - string 10 | --- 11 | $text = file_get_contents('/tmp/hello-world.txt'); 12 | -------------------------------------------------------------------------------- /php/file-write: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - file 5 | - filesystem 6 | - io 7 | - php 8 | - string 9 | - write 10 | --- 11 | $numBytes = file_put_contents('Hello, world!', '/tmp/hello-world.txt'); 12 | -------------------------------------------------------------------------------- /php/function: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - func 5 | - function 6 | - php 7 | --- 8 | function add(int $x, int $y): int { 9 | return $x + $y; 10 | } 11 | -------------------------------------------------------------------------------- /php/function-closure: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - anonymous 5 | - closure 6 | - function 7 | - lambda 8 | - php 9 | --- 10 | 'bar'], JSON_PRETTY_PRINT); 11 | -------------------------------------------------------------------------------- /php/json-unserialize: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - decode 5 | - json 6 | - php 7 | - unmarshall 8 | - unserialize 9 | --- 10 | $obj = json_decode('{"foo":"bar"}'); 11 | -------------------------------------------------------------------------------- /php/loop-do-while: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - control 5 | - loop 6 | - php 7 | - while 8 | --- 9 | $i = 0; 10 | do { 11 | // echo $i; 12 | } while ($i > 0); 13 | -------------------------------------------------------------------------------- /php/loop-for: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - control 5 | - for 6 | - loop 7 | - php 8 | --- 9 | for ($i = 1; $i <= 10; $i++) { 10 | // echo $i; 11 | } 12 | -------------------------------------------------------------------------------- /php/loop-foreach: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - control 5 | - for 6 | - loop 7 | - php 8 | --- 9 | foreach (['a', 'b', 'c'] as $idx => $val) { 10 | // echo "$idx: $val\n"; 11 | } 12 | -------------------------------------------------------------------------------- /php/loop-while: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - control 5 | - loop 6 | - php 7 | - while 8 | --- 9 | while (true) { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /php/null-coalesce: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - coalesce 5 | - conditional 6 | - null 7 | - php 8 | --- 9 | $username = $_GET['user'] ?? 'nobody'; 10 | -------------------------------------------------------------------------------- /php/pattern-singleton: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/regex-test: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/require: -------------------------------------------------------------------------------- 1 | TODO 2 | -------------------------------------------------------------------------------- /php/string-heredoc: -------------------------------------------------------------------------------- 1 | --- 2 | syntax: php 3 | tags: 4 | - heredoc 5 | - php 6 | - string 7 | --- 8 | $str = <<