Snippets:

  • Timezone

    A quick and easy one liner to access the current timezone!

    1new Intl.DateTimeFormat().resolvedOptions().timeZone
    1new Intl.DateTimeFormat().resolvedOptions().timeZone
  • Prettify

    1// Reference: https://www.totaltypescript.com/concepts/the-prettify-helper
    2
    3type Prettify<T> = {
    4 [K in keyof T]: T[K];
    5} & {};
    1// Reference: https://www.totaltypescript.com/concepts/the-prettify-helper
    2
    3type Prettify<T> = {
    4 [K in keyof T]: T[K];
    5} & {};
  • yw

    1# Bash script to execute a package.json script within a workspace
    2yw() {
    3 yarn workspace $(yarn workspaces list --json | jq -r .name | fzf) "$@"
    4}
    5
    6# For yarn v1 monorepos, I use ywold:
    7ywold() {
    8 yarn workspace $(yarn --json workspaces info | jq '.data' -r | jq "[keys][0] []" -r | fzf) $@
    9}
    1# Bash script to execute a package.json script within a workspace
    2yw() {
    3 yarn workspace $(yarn workspaces list --json | jq -r .name | fzf) "$@"
    4}
    5
    6# For yarn v1 monorepos, I use ywold:
    7ywold() {
    8 yarn workspace $(yarn --json workspaces info | jq '.data' -r | jq "[keys][0] []" -r | fzf) $@
    9}
  • Turbo workspace timings

    If you work within a monorepo and want to get a breakdown of the time that each workspace takes to run a task (lib:build in the below example), then these three shell commands will do that!

    1yarn turbo run lib:build --summarize
    2SUMMARY_FILE=$(/bin/ls .turbo/runs/*.json | head -n1)
    3cat $SUMMARY_FILE | jq '[.tasks[] | {"taskId": .taskId, "duration": (.execution.endTime - .execution.startTime)}] | sort_by(-.duration)'
    1yarn turbo run lib:build --summarize
    2SUMMARY_FILE=$(/bin/ls .turbo/runs/*.json | head -n1)
    3cat $SUMMARY_FILE | jq '[.tasks[] | {"taskId": .taskId, "duration": (.execution.endTime - .execution.startTime)}] | sort_by(-.duration)'
  • Alea

    1// Reference: https://www.npmjs.com/package/alea
    2// From http://baagoe.com/en/RandomMusings/javascript/
    3
    4// importState to sync generator states
    5Alea.importState = function(i){
    6 var random = new Alea();
    7 random.importState(i);
    8 return random;
    9};
    10
    11function Alea() {
    12 return function(...args) {
    13 // Johannes Baagøe <baagoe@baagoe.com>, 2010
    14 var s0 = 0;
    15 var s1 = 0;
    16 var s2 = 0;
    17 var c = 1;
    18
    19 if (args.length == 0) {
    20 args = [+new Date];
    21 }
    22 var mash = Mash();
    23 s0 = mash(' ');
    24 s1 = mash(' ');
    25 s2 = mash(' ');
    26
    27 for (var i = 0; i < args.length; i++) {
    28 s0 -= mash(args[i]);
    29 if (s0 < 0) {
    30 s0 += 1;
    31 }
    32 s1 -= mash(args[i]);
    33 if (s1 < 0) {
    34 s1 += 1;
    35 }
    36 s2 -= mash(args[i]);
    37 if (s2 < 0) {
    38 s2 += 1;
    39 }
    40 }
    41 mash = null;
    42
    43 var random = function() {
    44 var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
    45 s0 = s1;
    46 s1 = s2;
    47 return s2 = t - (c = t | 0);
    48 };
    49 random.next = random;
    50 random.uint32 = function() {
    51 return random() * 0x100000000; // 2^32
    52 };
    53 random.fract53 = function() {
    54 return random() +
    55 (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
    56 };
    57 random.version = 'Alea 0.9';
    58 random.args = args;
    59
    60 // my own additions to sync state between two generators
    61 random.exportState = function(){
    62 return [s0, s1, s2, c];
    63 };
    64 random.importState = function(i){
    65 s0 = +i[0] || 0;
    66 s1 = +i[1] || 0;
    67 s2 = +i[2] || 0;
    68 c = +i[3] || 0;
    69 };
    70
    71 return random;
    72
    73 };
    74}
    75
    76function Mash() {
    77 var n = 0xefc8249d;
    78
    79 var mash = function(data) {
    80 data = data.toString();
    81 for (var i = 0; i < data.length; i++) {
    82 n += data.charCodeAt(i);
    83 var h = 0.02519603282416938 * n;
    84 n = h >>> 0;
    85 h -= n;
    86 h *= n;
    87 n = h >>> 0;
    88 h -= n;
    89 n += h * 0x100000000; // 2^32
    90 }
    91 return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
    92 };
    93
    94 mash.version = 'Mash 0.9';
    95 return mash;
    96}
    97
    98export default Alea;
    1// Reference: https://www.npmjs.com/package/alea
    2// From http://baagoe.com/en/RandomMusings/javascript/
    3
    4// importState to sync generator states
    5Alea.importState = function(i){
    6 var random = new Alea();
    7 random.importState(i);
    8 return random;
    9};
    10
    11function Alea() {
    12 return function(...args) {
    13 // Johannes Baagøe <baagoe@baagoe.com>, 2010
    14 var s0 = 0;
    15 var s1 = 0;
    16 var s2 = 0;
    17 var c = 1;
    18
    19 if (args.length == 0) {
    20 args = [+new Date];
    21 }
    22 var mash = Mash();
    23 s0 = mash(' ');
    24 s1 = mash(' ');
    25 s2 = mash(' ');
    26
    27 for (var i = 0; i < args.length; i++) {
    28 s0 -= mash(args[i]);
    29 if (s0 < 0) {
    30 s0 += 1;
    31 }
    32 s1 -= mash(args[i]);
    33 if (s1 < 0) {
    34 s1 += 1;
    35 }
    36 s2 -= mash(args[i]);
    37 if (s2 < 0) {
    38 s2 += 1;
    39 }
    40 }
    41 mash = null;
    42
    43 var random = function() {
    44 var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
    45 s0 = s1;
    46 s1 = s2;
    47 return s2 = t - (c = t | 0);
    48 };
    49 random.next = random;
    50 random.uint32 = function() {
    51 return random() * 0x100000000; // 2^32
    52 };
    53 random.fract53 = function() {
    54 return random() +
    55 (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
    56 };
    57 random.version = 'Alea 0.9';
    58 random.args = args;
    59
    60 // my own additions to sync state between two generators
    61 random.exportState = function(){
    62 return [s0, s1, s2, c];
    63 };
    64 random.importState = function(i){
    65 s0 = +i[0] || 0;
    66 s1 = +i[1] || 0;
    67 s2 = +i[2] || 0;
    68 c = +i[3] || 0;
    69 };
    70
    71 return random;
    72
    73 };
    74}
    75
    76function Mash() {
    77 var n = 0xefc8249d;
    78
    79 var mash = function(data) {
    80 data = data.toString();
    81 for (var i = 0; i < data.length; i++) {
    82 n += data.charCodeAt(i);
    83 var h = 0.02519603282416938 * n;
    84 n = h >>> 0;
    85 h -= n;
    86 h *= n;
    87 n = h >>> 0;
    88 h -= n;
    89 n += h * 0x100000000; // 2^32
    90 }
    91 return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
    92 };
    93
    94 mash.version = 'Mash 0.9';
    95 return mash;
    96}
    97
    98export default Alea;