Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 8, 2024
1 parent a9201f2 commit ec6216d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 122 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
singleQuote: true
semi: false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -22,15 +22,15 @@ 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

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)
Expand Down
74 changes: 37 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
}
156 changes: 78 additions & 78 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit ec6216d

Please sign in to comment.