# Session 3: Utility Structured Types [slide online](https://hackmd.io/@e8KGkGmbTPWKGH_D_dy9DA/ByqkuZJPs) --- ## Date ### Constructor - `Date()` - Equivalent to `new Date().toString()` - `new Date([<epochMs|dateSting|dateObj>])` - `new Date([year, ..., ms])` --- ## Date ### Main static methods - `Date.now()` - ms elapsed since January 1, 1970 00:00:00 UTC --- ## Date ### Main instance methods > Regular getters - `<date>.getDate()` - day of the month - `<date>.getDay()` - day of the week (according to local) - `<date>.getFullYear()` - `<date>.getHours()` --- ## Date ### Main instance methods > Regular getters - `<date>.getMilliseconds()` - `<date>.getMinutes()` - `<date>.getMonth()` - `<date>.getSeconds()` - `<date>.getTime()` - ms elapsed since January 1, 1970 00:00:00 UTC --- ## Date ### Main instance methods > Regular getters - `<date>.getTimezoneOffset()` - in minutes --- ## Date ### Main instance methods > UTC getters - `<date>.getUTCDate()` - day of the month - `<date>.getUTCDay()` - day of the week (according to local) - `<date>.getUTCFullYear()` - `<date>.getUTCHours()` --- ## Date ### Main instance methods > UTC getters - `<date>.getUTCMilliseconds()` - `<date>.getUTCMinutes()` - `<date>.getUTCMonth()` - `<date>.getUTCSeconds()` --- ## Date ### Main instance methods > Regular setters - `<date>.setDate(<dateNumber>)` - `<date>.setFullYear(<year>[, <monthNumber>[, <dateNumber>]])` - `<date>.setHours(<hours>[, <min>[, <seconds>[, <milliseconds>]]])` --- ## Date ### Main instance methods > Regular setters - `<date>.setMilliseconds(<ms>)` - `<date>.setMinutes(<min>[, <seconds>[, <milliseconds>]])` - `<date>.setMonth(<monthNumber>)` - `<date>.setSeconds(<seconds>[, <milliseconds>])` - `<date>.setTime(<epochMs>)` --- ## Date ### Main instance methods > UTC setters - `<date>.setUTCDate(<dateNumber>)` - `<date>.setUTCFullYear(<year>[, <monthNumber>[, <dateNumber>]])` - `<date>.setUTCHours(<hours>[, <min>[, <seconds>[, <milliseconds>]]])` --- ## Date ### Main instance methods > UTC setters - `<date>.setUTCMilliseconds(<ms>)` - `<date>.setUTCMinutes(<min>[, <seconds>[, <milliseconds>]])` - `<date>.setUTCMonth(<monthNumber>)` - `<date>.setUTCSeconds(<seconds>[, <milliseconds>])` --- ## Date ### Main instance methods > String converters - `<date>.toDateString()` - "Thu Nov 24 2022" - `<date>.toISOString()` - "2022-11-24T13:52:16.055Z" - `<date>.toLocaleDateString()` - "11/24/2022", "24/11/2022" --- ## Date ### Main instance methods > String converters - `<date>.toLocaleString()` - "11/24/2022, 8:52:16 AM" - `<date>.toLocaleTimeString()` - "8:52:16 AM" - `<date>.toString()` - "Thu Nov 24 2022 08:52:16 GMT-0500 (Colombia Standard Time)" --- ## Date ### Main instance methods > String converters - `<date>.toTimeString()` - "08:52:16 GMT-0500 (Colombia Standard Time)" - `<date>.toUTCString()` - "Thu, 24 Nov 2022 13:52:16 GMT" --- ## Date ### Alternatives - [Moment.js](https://momentjs.com/) - [`Temporal`](https://tc39.es/proposal-temporal/docs/index.html) (proposal) --- ## Math ### Main static properties - `Math.E` - `Math.PI` --- ## Math ### Main static methods > Integers - `Math.ceil(<number>)` - `Math.floor(<number>)` - `Math.round(<number>)` - `Math.trunc(<number>)` --- ## Math ### Main static methods > Basic functions - `Math.abs(<number>)` - `Math.pow(<base>, <exponent>)` - `Math.exp(<number>)` - `Math.pow(Math.E, <number>)` - `Math.sqrt(<number>)` --- ## Math ### Main static methods > Aggregation functions - `Math.max(numbers...)` - `Math.min(numbers...)` --- ## Math ### Main static methods > Logarithms - `Math.log(<number>)` - `Math.log1p(<number>)` - `Math.log10(<number>)` - `Math.log2(<number>)` --- ## Math ### Main static methods > Trigonometry - `Math.sin(<radians>)` - `Math.asin(<number>)` - `Math.cos(<radians>)` - `Math.acos(<number>)` - `Math.tan(<radians>)` - `Math.atan(<number>)` --- ## Math ### Main static methods > Hyperbolic functions - `Math.sinhh(<number>)` - `Math.asinh(<number>)` - `Math.cosh(<number>)` - `Math.acosh(<number>)` - `Math.tanh(<number>)` - `Math.atanh(<number>)` --- ## Math ### Main static methods > Misc - `Math.random()` - ⚠️**NOT** cryptographically secure - `Math.hypot(numbers...)` --- ## RegExp ### Literal notation and constructor ```javascript const simpleRegex = /w/; const sameSimpleRegex = Regex(/w/); const sameSimpleRegexAgain = new Regex(/w/); const simpleRegexWithFlag = /w/i; const sameSimpleRegexWithFlag = Regex(/w/, "i"); const rameSimpleRegexWithFlagAgain = new Regex(/w/, "i"); ``` --- ## RegExp ### Literal notation and constructor ```javascript const testString = "Whatever whatever"; console.log([...testString.matchAll(simpleRegex)]); // ["w"] console.log([...testString.matchAll(simpleRegexWithFlag)]); // ["W", "w"] ``` --- ## RegExp ## Constructor syntax - `[new] Regex(<pattern>[, <flags>])` - `<pattern>` can be a string or a regex literal describing a pattern - `<flags>` is a string --- ## RegExp ### Pattern matching syntax ```javascript /<patternDefinition>/<flags> // patternDefinition describes the pattern used to find matches // flags modify the application of the pattern when looking for matches ``` --- ## RegExp ### Pattern matching syntax ```mermaid flowchart LR 0(Regex syntax)-->1(Pattern definition) 0-->2(Flags) 1-->3(Character classes) 1-->4(Assertions) 1-->5(Groups and backreferences) 4-->6(Boundary-type assertions) 4-->7(Other assertions) ``` --- ## RegExp > Pattern matching syntax ### Common tokens and character classes - Character class / negated: `[xyz]`, `[a-c]` / `[^xyz]`, `[^a-c]` - Dot wildcard: `.` - Digit / negated: `\d` / `\D` --- ## RegExp > Pattern matching syntax ### Common tokens and character classes - Alphanumeric character / negated: `\w` / `\W` - Special / not special character: `\` - Disjuntion: `x|y` --- ## RegExp > Pattern matching syntax ### Common tokens and character classes - Blank character / negated: `\s` / `\S` - Tab: `\t` - Linefeed: `\n` - Carriage return: `\r` --- ## RegExp > Pattern matching syntax ### Boundary-type assertions - Beginning of input / line: `^` - End of input / line: `$` - Word boundary / negated: `\b` / `\B` --- ## RegExp > Pattern matching syntax ### Lookahead and lookbehind assertions - Lookahead / negated: `x(?=y)` / `x(?!y)` - Lookbehind / negated: `(?<=y)x` / `(?<!y)x` --- ## RegExp > Pattern matching syntax ### Groups and backreferences - Anonymous capturing group: `(x)` - Reference to anonymous capturing group: `\<index>` - Non-capturing group: `(?:x)` --- ## RegExp > Pattern matching syntax ### Groups and backreferences - Named capturing group: `(?<name>x)` - `<` and `>` are part of the syntax - Can be found in the `groups` key of the object returned by `<string>.matchAll()` - Reference to named capturing group: `\k<name>` --- ## RegExp > Pattern matching syntax ### Quantifiers - "x" present 0 or more times: `x*` - "x" present 1 or more times: `x+` - "x" present 0 or 1 times: `x?` --- ## RegExp > Pattern matching syntax ### Quantifiers - "x" present _exactly_ n times: `x{n}` - "x" present n or more times: `x{n,}` - "x" present between n and m times: `x{n,m}` --- ## RegExp > Pattern matching syntax ### Quantifiers > Greedy vs non-greedy ```javascript const greedy = /\d+/; const nonGreedy = /\d+?/; const testString = "1234"; console.log(testString.match(greedy)); console.log(testString.match(nonGreedy)); ``` --- ## RegExp > Pattern matching syntax ### Most importat flags - Global: `g` - Ignore case: `i` - Multiline: `m` - Allows ^ and $ to match newline characters - Dot all: `s` - Allows `.` to match newline characters --- ## RegExp ### Main instance properties - `<regExp>.flags` --- ## RegExp ### Main instance methods - `<regExp>.test(<string>)` - Returns a boolean --- ## RegExp ### Miscellaneous notes - Useful tool: [https://regex101.com/](https://regex101.com/) - Related string methods: `match()`, `matchAll()`, `replace()`, `replaceAll()`, `search()`, and `split()` --- ## Error ### Constructor - `[new] Error([<message>[, <options>]])` - `<options>` is an object with these properties: - `cause` --- ## Error ### Main instance properties - `<error>.message` - `<error>.name` --- ## Error ### Main instance methods - `<error>.toString()` - "<ErrorName>: <message>" --- ## Error ### Main error types - `RangeError` - Numeric value or parameter outside of valid range - `ReferenceError` - A reference doesn't exist in the context - `SyntaxError` --- ## Error ### Main error types - `TypeError` - An operation could not be performed because of an incorrect operand - `URIError` - `AggregateError` - Reporting multiple errors --- ## JSON ### Static methods - `JSON.parse()` - `JSON.stringify()` --- ## Wraping up - Topics covered - Date - Math - RegExp - Error - JSON - Exercises - Next session - Coming up... - Schedule ---
{"metaMigratedAt":"2023-06-17T15:23:41.219Z","metaMigratedFrom":"YAML","title":"Session 3: Utility Structured types","breaks":true,"contributors":"[{\"id\":\"7bc28690-699b-4cf5-8a18-7fc3fddcbd0c\",\"add\":9141,\"del\":0}]"}
    185 views