Skip to content

Commit da765ec

Browse files
committed
add basic design for allowing to set letterCase
1 parent b24d582 commit da765ec

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

src/model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface ModelConfiguration {
4747
jwt: string
4848
jwtStorage: string | false
4949
camelizeKeys: boolean
50+
letterCase: string
5051
strictAttributes: boolean
5152
logger: ILogger
5253
}
@@ -129,6 +130,7 @@ export class JSORMBase {
129130
static isBaseClass: boolean
130131
static jwt?: string
131132
static camelizeKeys: boolean = true
133+
static letterCase: string = "underscore"
132134
static strictAttributes: boolean = false
133135
static logger: ILogger = defaultLogger
134136

@@ -408,7 +410,7 @@ export class JSORMBase {
408410
this.reset()
409411
}
410412

411-
reset() : void {
413+
reset(): void {
412414
this._originalAttributes = cloneDeep(this._attributes)
413415
this._originalRelationships = this.relationshipResourceIdentifiers(
414416
Object.keys(this.relationships)

src/util/write-payload.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { JSORMBase, ModelRecord } from "../model"
22
import { IncludeDirective, IncludeScopeHash } from "./include-directive"
33
import { IncludeScope } from "../scope"
44
import { tempId } from "./temp-id"
5-
import { underscore } from "inflected"
5+
import { underscore, dasherize } from "inflected"
66
import {
77
JsonapiRequestDoc,
88
JsonapiResourceIdentifier,
@@ -33,10 +33,8 @@ export class WritePayload<T extends JSORMBase> {
3333
const attrs: ModelRecord<T> = {}
3434

3535
this._eachAttribute((key, value) => {
36-
const snakeKey = underscore(key)
37-
3836
if (!this.model.isPersisted || this.model.changes()[key]) {
39-
attrs[snakeKey] = value
37+
attrs[this._letterCaseKey(key)] = value
4038
}
4139
})
4240

@@ -88,8 +86,8 @@ export class WritePayload<T extends JSORMBase> {
8886
const nested = (<any>this.includeDirective)[key]
8987

9088
let idOnly = false
91-
if (key.indexOf('.') > -1) {
92-
key = key.split('.')[0]
89+
if (key.indexOf(".") > -1) {
90+
key = key.split(".")[0]
9391
idOnly = true
9492
}
9593

@@ -101,8 +99,8 @@ export class WritePayload<T extends JSORMBase> {
10199
relatedModels.forEach(relatedModel => {
102100
if (
103101
idOnly ||
104-
this.model.hasDirtyRelation(key, relatedModel) ||
105-
relatedModel.isDirty(nested)
102+
this.model.hasDirtyRelation(key, relatedModel) ||
103+
relatedModel.isDirty(nested)
106104
) {
107105
data.push(this._processRelatedModel(relatedModel, nested, idOnly))
108106
}
@@ -115,15 +113,15 @@ export class WritePayload<T extends JSORMBase> {
115113
// (maybe the "department" is not dirty, but the employee changed departments
116114
if (
117115
idOnly ||
118-
this.model.hasDirtyRelation(key, relatedModels) ||
119-
relatedModels.isDirty(nested)
116+
this.model.hasDirtyRelation(key, relatedModels) ||
117+
relatedModels.isDirty(nested)
120118
) {
121119
data = this._processRelatedModel(relatedModels, nested, idOnly)
122120
}
123121
}
124122

125123
if (data) {
126-
_relationships[underscore(key)] = { data }
124+
_relationships[this._letterCaseKey(key)] = { data }
127125
}
128126
}
129127
})
@@ -169,7 +167,11 @@ export class WritePayload<T extends JSORMBase> {
169167

170168
// private
171169

172-
private _processRelatedModel(model: T, nested: IncludeScopeHash, idOnly: boolean) {
170+
private _processRelatedModel(
171+
model: T,
172+
nested: IncludeScopeHash,
173+
idOnly: boolean
174+
) {
173175
model.clearErrors()
174176

175177
if (!model.isPersisted) {
@@ -252,4 +254,11 @@ export class WritePayload<T extends JSORMBase> {
252254
}
253255
})
254256
}
257+
258+
private _letterCaseKey(key): string {
259+
if (this.model.klass.letterCase == "dasherized") {
260+
return dasherize(key)
261+
}
262+
return underscore(key)
263+
}
255264
}

test/fixtures.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export class PersonWithoutCamelizedKeys extends Person {
3535
@Attr first_name: string
3636
}
3737

38+
@Model({ letterCase: "dasherized" })
39+
export class PersonWithDasherizedKeys extends Person {}
40+
3841
@Model({
3942
endpoint: "/v1/authors",
4043
jsonapiType: "authors"

test/unit/model-attributes.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ describe("Model attributes", () => {
2020
expect(person.firstName).to.eq("Joe")
2121
})
2222

23+
xit("camelizes dasherized strings", function() {
24+
const person = new Person({ "first-name": "Joe" })
25+
expect(person.firstName).to.eq("Joe")
26+
})
27+
2328
it("does not camlize underscored strings if camelization is disabled", () => {
2429
const person = new PersonWithoutCamelizedKeys({ first_name: "Joe" })
2530
expect(person.firstName).to.eq(undefined)

test/unit/write-payload.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { sinon, expect } from "../test-helper"
2+
import { WritePayload } from "../../src/util/write-payload"
3+
import { Person, PersonWithDasherizedKeys } from "../fixtures"
4+
5+
describe("WritePayload", () => {
6+
it("underscores attributes", function() {
7+
let person = new Person({ first_name: "Joe" })
8+
let payload = new WritePayload(person, true)
9+
expect(payload.asJSON()).to.deep.equal({
10+
data: {
11+
type: "people",
12+
attributes: {
13+
first_name: "Joe"
14+
}
15+
}
16+
})
17+
})
18+
19+
xit("dasherizes attributes", function() {
20+
let person = new PersonWithDasherizedKeys({ first_name: "Joe" })
21+
let payload = new WritePayload(person, true)
22+
expect(payload.asJSON()).to.deep.equal({
23+
data: {
24+
type: "people",
25+
attributes: {
26+
"first-name": "Joe"
27+
}
28+
}
29+
})
30+
})
31+
})

0 commit comments

Comments
 (0)