Skip to content

Commit

Permalink
docs ~ refactor Deno example to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Jan 15, 2021
1 parent ed5af63 commit 8901b74
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
30 changes: 15 additions & 15 deletions eg/show-paths.local.deno.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env es6, deno */
// # spell-checker:ignore Deno

/* eslint-disable no-console , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
/* eslint-disable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */

// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path='../src/types/deno.d.ts'/>
Expand All @@ -13,25 +13,25 @@ import osPaths from '../src/mod.deno.ts';
// @ts-ignore
const deno = Deno;

console.log({ osPaths });
function objectEntries(obj: any) {
const map: any = {};
Object.keys(obj).forEach((key) => {
const value = obj[key];
const val = typeof value === 'function' ? value() : value;
map[key] = val;
});
return map;
}

Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log({ osPaths });
console.log(objectEntries(osPaths));

deno.env.set('TEMP', 'temp');
deno.env.set('TMPDIR', 'tmpdir');
deno.env.set('TMP', 'tmp');
Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log(objectEntries(osPaths));

deno.env.set('TEMP', 'NEW');
Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log(objectEntries(osPaths));

/* eslint-enable no-console , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
/* eslint-enable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
33 changes: 17 additions & 16 deletions eg/show-paths.remote.deno.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
/* eslint-env es6, deno */
// # spell-checker:ignore Deno

/* eslint-disable no-console , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
/* eslint-disable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */

// @ts-ignore
import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@5b6b93bdb048098b86f9b8f0952180c2bf0532b1/src/mod.deno.ts';
// import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@5b6b93bdb048098b86f9b8f0952180c2bf0532b1/src/mod.deno.ts';
// import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@2d5924e692617a575c8a73c9fb611e93085c4339/src/mod.deno.ts';
// import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@latest/src/mod.deno.ts';
import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@6.0/src/mod.deno.ts';

// create a local reference to refer to `Deno` (for better linting without need for multiple `// @ts-ignore` directives)
// @ts-ignore
const deno = Deno;

console.log({ osPaths });
function objectEntries(obj: any) {
const map: any = {};
Object.keys(obj).forEach((key) => {
const value = obj[key];
const val = typeof value === 'function' ? value() : value;
map[key] = val;
});
return map;
}

Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log({ osPaths });
console.log(objectEntries(osPaths));

deno.env.set('TEMP', 'temp');
deno.env.set('TMPDIR', 'tmpdir');
deno.env.set('TMP', 'tmp');
Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log(objectEntries(osPaths));

deno.env.set('TEMP', 'NEW');
Object.keys(osPaths).forEach((key) => {
const value = (osPaths as any)[key]();
console.log({ key, value });
});
console.log(objectEntries(osPaths));

/* eslint-enable no-console , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
/* eslint-enable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */

0 comments on commit 8901b74

Please sign in to comment.