Show Menu
Cheatography

Angular/Typescript/Javascript Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Javascript

Find text in strings
"­Hello world".i­nc­lud­es(­"­wor­ld"); // True
String to Int
var a = parseI­nt(­"­10")
Int to String
value.t­oS­tring()
or
"­" + value
or
String­(value)
Switch statement
switch­(ex­pre­ssion) {
    case n:
        code block
        break;
    default:
        code block
}
declare var as function
var a: Function
call object method as parameter
function bar(obj: Foo, func: string) {
    obj[func](); }
Find string in array of strings
var strs = ['abc', 'def', 'ghi'];
strs.find(str => {
    return str === 'abc'
} );
Creating a Promise
myProm = new Promis­e((res, rej) => {
    // something asynch­ronous then call:
    res(someValue); // or
    rej("failure reason");
})
Creating an Observable
let obs = Observ­abl­e.c­rea­te(obs => {
    setTimeout(() => {
        obs.next("data to send");
        console.log("am done");
        obs.complete();// we are done
        // obs.er­ror(new Error("msg"));
    }, 2000);
})
Loop through object
for (const v of Object.ke­ys(vs)) {
    r.push(vs[v]) }
Parse string to object
JSON.p­arse()
Is variable an array
Array.i­sA­rray()
case statement
switch­(ex­pre­ssion) {
    case n:
        code block
        break;
    default:
        code block
}
Test for empty object
Object.ke­ys(­obj­).l­ength === 0

Angular CLI

Build in test mode
ng build --envi­ron­men­t=test
Start server
lite-s­erver
lite-s­erver config­uration
"­fil­es": ["./­dis­t/*.{j­s}"],    
"­ser­ver­": { "­bas­eDi­r": "./d­ist­" }

Angular

@NgModule({
    providers
    declarations
    imports

    exports

    entryComponents

    bootstrap

    schemas
    id
})

- injectable objects, can appear in the constructor
- list of directives/pipes
- list of modules whose exported direct­ive­s/pipes should be available to templates in this module
- direct­ive­s/p­ipe­s/m­odules used in template of components part of module importing this module
- list of components that should be compiled when this module is defined
- components that should be bootst­rapped when this module is bootstrapped

Python

Ternary operator
a if condition else b
Remove array element
remove first matching value
[0, 2, 3, 2].rem­ove(2) => [0, 3, 2]
remove a specific index
[3, 2, 2, 1].del a[1] => [3, 2, 1]
return the removed element
[4, 3, 5].pop(1) returns 3 => [4, 5]
Item in list
someEl­ement in someList
Print tuple
print "{0}, {1}, {2}".fo­rma­t(*[1, 2, 3])
Flatten tuple
[el for tup in ((1, 2), (3,)) for el in tup]
=> [1, 2, 3]
Index in list
["fo­o", "­bar­", "­baz­"­].i­nde­x("b­ar")
regex replace
re.sub­(pa­ttern, repl, string, count=0, flags=0)
print array
print ', '.join­(names)
for loop with index
for idx, val in enumer­ate­(list):
for in - val is the value (not index)
for val in list
int to string
str(nu­mber)
find substring
string.fi­nd(­'su­bst­ring')

HTML

Cast Elements to HTMLEl­ements
elems  as HTMLCo­lle­cti­onO­f<H­TML­Ele­men­t>