Skip to content

Instantly share code, notes, and snippets.

@renoirb
Created February 28, 2020 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/a08db4fe1d0e65a24bb95be3aeb33050 to your computer and use it in GitHub Desktop.
Save renoirb/a08db4fe1d0e65a24bb95be3aeb33050 to your computer and use it in GitHub Desktop.
Example using self-contained published package
From 4a22bd3338241b30edddb0bffab035b0b1ba3a99 Mon Sep 17 00:00:00 2001
From: Renoir Boulanger <hello@renoirboulanger.com>
Date: Fri, 28 Feb 2020 16:37:39 -0500
Subject: [PATCH] Example
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..239ecff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+yarn.lock
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..a92a16c
--- /dev/null
+++ b/index.js
@@ -0,0 +1,34 @@
+const {
+ dirnameNormalizer,
+ createAssetReference,
+} = require('url-dirname-normalizer')
+
+const dirnames = [
+ ['http://example.org/foo.action?bar=bazz', 'example.org/foo/bar/bazz'],
+ [
+ 'http://example.org/@ausername/some-lengthy-string-ending-with-a-hash-1a2d8a61510',
+ 'example.org/ausername/some-lengthy-string-ending-with-a-hash',
+ ],
+]
+
+for (const [input, expected] of dirnames) {
+ const output = dirnameNormalizer(input)
+ const worked = output === expected
+ console.log('dirnames', { input, output, worked, expected })
+}
+
+const assets = [
+ [
+ 'https://blackhole.webpagetest.org/renoirb/archivator/test/normalizer/assets/ignore-path-tls',
+ '//example.org/a/b.png',
+ 'https://example.org/a/b.png',
+ 'Ignore document origin if resource has full URL, protocol relative, over TLS',
+ ],
+]
+
+for (const [sourceDocument, input, expected] of assets) {
+ const assetReferenceHelper = createAssetReference(sourceDocument)
+ const output = assetReferenceHelper(input)
+ const worked = output.src === expected
+ console.log('assets', { sourceDocument, input, output, worked, expected })
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..02b756e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,16 @@
+{
+ "name": "using-particles",
+ "version": "1.0.0",
+ "main": "index.js",
+ "scripts": {
+ "dev": "node index.js",
+ "fix": "use-prettier --write '*.js'"
+ },
+ "devDependencies": {
+ "@renoirb/conventions-use-prettier": "^1.0.1"
+ },
+ "dependencies": {
+ "url-dirname-normalizer": "^1.1.0",
+ "@babel/runtime-corejs3": "^7.8.0"
+ }
+}
diff --git a/prettier.config.js b/prettier.config.js
new file mode 100644
index 0000000..81c93e8
--- /dev/null
+++ b/prettier.config.js
@@ -0,0 +1,6 @@
+/* eslint-disable @typescript-eslint/no-var-requires */
+const base = require('@renoirb/conventions-use-prettier')
+
+module.exports = {
+ ...base,
+}
--
2.21.0
@renoirb
Copy link
Author

renoirb commented Feb 28, 2020

What ?

This is a quick copy-pasta example of how to use a TypeScript package published with typings. Just as a quick demo about TypeScript's code completion in VSCode.

Also, that's a quick example of that the build output of that, while written in modern ES2019 with TypeScript 3.7, we can still import and use quickly.

note URL
source https://gitlab.com/renoirb/renoirb-particles/-/tree/master/apps/url-dirname-normalizer
package https://www.npmjs.com/package/url-dirname-normalizer

Use example

mkdir -p using-url-dirname-normalizer
cd using-url-dirname-normalizer
curl -sSL https://gist.githubusercontent.com/renoirb/a08db4fe1d0e65a24bb95be3aeb33050/raw/eec76e8fa1b138fdb3f148886e8d360763c31578/example.patch -o example.patch
patch -i example.patch
yarn
node index.js

Wanna add more test-cases? Read on and play with other examples from apps/url-dirname-normalizer/resources/fixtures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment