From ec6216d8cf7566799111790f70973739f0799730 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 8 Apr 2024 12:47:35 -0700 Subject: [PATCH] prettier --- .github/dependabot.yml | 6 +- .github/workflows/codeql.yml | 2 +- .prettierrc.yml | 2 + README.md | 6 +- index.js | 74 ++++++++--------- test/index.js | 156 +++++++++++++++++------------------ 6 files changed, 124 insertions(+), 122 deletions(-) create mode 100644 .prettierrc.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index feb90ba..c9826f9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,7 +5,7 @@ version: 2 updates: - - package-ecosystem: "npm" - directory: "/" # Location of package manifests + - package-ecosystem: 'npm' + directory: '/' # Location of package manifests schedule: - interval: "weekly" + interval: 'weekly' diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 7e97bed..8314a66 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -7,7 +7,7 @@ on: # The branches below must be a subset of the branches above branches: [master] schedule: - - cron: "18 7 * * 4" + - cron: '18 7 * * 4' jobs: codeql: diff --git a/.prettierrc.yml b/.prettierrc.yml new file mode 100644 index 0000000..8ded5e0 --- /dev/null +++ b/.prettierrc.yml @@ -0,0 +1,2 @@ +singleQuote: true +semi: false diff --git a/README.md b/README.md index 1468908..ac35779 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Notes are objects that exist on Haraka connections and transactions. Prior to th Sets a note at a dot delimited path to the specified value. The path can be any number of levels deep and any missing objects in the path are [autovivified](https://en.wikipedia.org/wiki/Autovivification). Perl refugees, contain yourselves. ```js -connection.transaction.notes.set("queue.wants", "smtp_forward"); +connection.transaction.notes.set('queue.wants', 'smtp_forward') ``` The above command sets `connection.transaction.notes.queue.wants` to the value 'smtp_forward'. @@ -22,7 +22,7 @@ The above command sets `connection.transaction.notes.queue.wants` to the value ' Fetches the value of a note from a given dot delimited path. ```js -connection.transaction.notes.get("queue.wants"); +connection.transaction.notes.get('queue.wants') ``` ## Array Syntax @@ -30,7 +30,7 @@ connection.transaction.notes.get("queue.wants"); The get and set functions support passing the path as an array of strings. This might be useful to the type of masochist that has dots in their JS/JSON keys. Example: ```js -connection.transaction.notes.get(["i.do", "like", "pa.in"]); +connection.transaction.notes.get(['i.do', 'like', 'pa.in']) ``` ## [Note Path Registry](https://github.com/haraka/haraka-notes/wiki) diff --git a/index.js b/index.js index 8bcf9d4..8ceacfc 100644 --- a/index.js +++ b/index.js @@ -1,54 +1,54 @@ class Notes { - constructor(notes) { - if (notes && typeof notes === "object") { - Object.assign(this, notes); + constructor(notes) { + if (notes && typeof notes === 'object') { + Object.assign(this, notes) + } + + Object.defineProperty(this, 'set', { + configurable: false, + enumerable: false, + writable: false, + value: assignPathValue.bind(this), + }) + + Object.defineProperty(this, 'get', { + configurable: false, + enumerable: false, + writable: false, + value: getPathValue.bind(this), + }) } - - Object.defineProperty(this, "set", { - configurable: false, - enumerable: false, - writable: false, - value: assignPathValue.bind(this), - }); - - Object.defineProperty(this, "get", { - configurable: false, - enumerable: false, - writable: false, - value: getPathValue.bind(this), - }); - } } -module.exports = Notes; +module.exports = Notes function getSegments(path) { - // a dot.delimited.path - if (typeof path === "string") return path.split("."); + // a dot.delimited.path + if (typeof path === 'string') return path.split('.') - // ['one', 'two', 'thr.ee'] - if (Array.isArray(path)) return path; + // ['one', 'two', 'thr.ee'] + if (Array.isArray(path)) return path } function assignPathValue(path, value) { - if (path === undefined || value === undefined) return; + if (path === undefined || value === undefined) return - const segments = getSegments(path); - let dest = this; + const segments = getSegments(path) + let dest = this - while (segments.length > 1) { + while (segments.length > 1) { // create any missing paths - if (!dest[segments[0]]) dest[segments[0]] = {}; - // set dest one path segment deeper - dest = dest[segments.shift()]; - } - dest[segments[0]] = value; + if (!dest[segments[0]]) dest[segments[0]] = {} + // set dest one path segment deeper + dest = dest[segments.shift()] + } + dest[segments[0]] = value } function getPathValue(path) { - if (!path) return; - const segments = getSegments(path); - return segments.reduce((prev, curr) => { - return prev ? prev[curr] : undefined; - }, this); + if (!path) return + const segments = getSegments(path) + return segments.reduce((prev, curr) => { + return prev ? prev[curr] : undefined + }, this) } diff --git a/test/index.js b/test/index.js index aa374bb..ebab3da 100644 --- a/test/index.js +++ b/test/index.js @@ -1,93 +1,93 @@ -const assert = require("assert"); +const assert = require('assert') -const Notes = require("../index"); +const Notes = require('../index') -describe("notes", () => { - beforeEach((done) => { - this.notes = new Notes(); - done(); - }); +describe('notes', () => { + beforeEach((done) => { + this.notes = new Notes() + done() + }) - it("exports an object", (done) => { + it('exports an object', (done) => { // console.log(this.notes) - assert.ok(typeof this.notes === "object"); - done(); - }); + assert.ok(typeof this.notes === 'object') + done() + }) - const functionList = ["get", "set"]; + const functionList = ['get', 'set'] - functionList.forEach((fn) => { - it(`has ${fn}()`, (done) => { - assert.equal(typeof this.notes[fn], "function"); - done(); - }); - }); + functionList.forEach((fn) => { + it(`has ${fn}()`, (done) => { + assert.equal(typeof this.notes[fn], 'function') + done() + }) + }) - functionList.forEach((fn) => { - it(`ignores attempts to redefine ${fn}`, (done) => { - this.notes[fn] = "turd"; - this.notes[fn]("turd"); - done(); - }); - }); + functionList.forEach((fn) => { + it(`ignores attempts to redefine ${fn}`, (done) => { + this.notes[fn] = 'turd' + this.notes[fn]('turd') + done() + }) + }) - it("sets a top level value", (done) => { - this.notes.set("foo", "bar"); - // console.log(this.notes) - assert.equal(this.notes.foo, "bar"); - done(); - }); + it('sets a top level value', (done) => { + this.notes.set('foo', 'bar') + // console.log(this.notes) + assert.equal(this.notes.foo, 'bar') + done() + }) - it("can set a false value", (done) => { - this.notes.set("boolean", false); - assert.equal(this.notes.boolean, false); - done(); - }); + it('can set a false value', (done) => { + this.notes.set('boolean', false) + assert.equal(this.notes.boolean, false) + done() + }) - it("gets a top level value", (done) => { - this.notes.set("foo", "bar"); - assert.equal(this.notes.get("foo"), "bar"); - done(); - }); + it('gets a top level value', (done) => { + this.notes.set('foo', 'bar') + assert.equal(this.notes.get('foo'), 'bar') + done() + }) - it("sets/gets a second level value", (done) => { - this.notes.set("seg1.seg2", "bar"); - assert.equal(this.notes.seg1.seg2, "bar"); - assert.equal(this.notes.get("seg1.seg2"), "bar"); - done(); - }); + it('sets/gets a second level value', (done) => { + this.notes.set('seg1.seg2', 'bar') + assert.equal(this.notes.seg1.seg2, 'bar') + assert.equal(this.notes.get('seg1.seg2'), 'bar') + done() + }) - it("sets/gets a three level value", (done) => { - this.notes.set("one.two.three", "floor"); - assert.equal(this.notes.one.two.three, "floor"); - assert.equal(this.notes.get("one.two.three"), "floor"); - done(); - }); + it('sets/gets a three level value', (done) => { + this.notes.set('one.two.three', 'floor') + assert.equal(this.notes.one.two.three, 'floor') + assert.equal(this.notes.get('one.two.three'), 'floor') + done() + }) - it("supports array syntax", (done) => { - this.notes.set(["one", "two", "three"], "floor"); - assert.equal(this.notes.one.two.three, "floor"); - assert.equal(this.notes.get(["one", "two", "three"]), "floor"); - done(); - }); + it('supports array syntax', (done) => { + this.notes.set(['one', 'two', 'three'], 'floor') + assert.equal(this.notes.one.two.three, 'floor') + assert.equal(this.notes.get(['one', 'two', 'three']), 'floor') + done() + }) - it("array syntax tolerates dots", (done) => { - this.notes.set(["one", "two", "three.four"], "floor"); - assert.equal(this.notes.one.two["three.four"], "floor"); - assert.equal(this.notes.get(["one", "two", "three.four"]), "floor"); - done(); - }); -}); + it('array syntax tolerates dots', (done) => { + this.notes.set(['one', 'two', 'three.four'], 'floor') + assert.equal(this.notes.one.two['three.four'], 'floor') + assert.equal(this.notes.get(['one', 'two', 'three.four']), 'floor') + done() + }) +}) -describe("notes + object", () => { - it("assigns instantiation object", (done) => { - const passIn = { - one: true, - two: "false", - three: "floor", - }; - this.notes = this.notes = new Notes(passIn); - assert.deepEqual(this.notes, passIn); - done(); - }); -}); +describe('notes + object', () => { + it('assigns instantiation object', (done) => { + const passIn = { + one: true, + two: 'false', + three: 'floor', + } + this.notes = this.notes = new Notes(passIn) + assert.deepEqual(this.notes, passIn) + done() + }) +})