Skip to content

Commit da0552e

Browse files
committed
make key converters static
1 parent 5da4167 commit da0552e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/model.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ export class JSORMBase {
468468
let attributeName = key
469469

470470
if (this.klass.camelizeKeys) {
471-
attributeName = this.deserializeKey(key)
471+
attributeName = this.klass.deserializeKey(key)
472472
}
473473

474474
if (key === "id" || this.klass.attributeList[attributeName]) {
@@ -694,6 +694,17 @@ export class JSORMBase {
694694
return this.baseClass
695695
}
696696

697+
static serializeKey(key: string): string {
698+
if (this.letterCase == "dasherized") {
699+
return dasherize(underscore(key))
700+
}
701+
return underscore(key)
702+
}
703+
704+
static deserializeKey(key: string): string {
705+
return camelize(underscore(key), false)
706+
}
707+
697708
async destroy(): Promise<boolean> {
698709
const url = this.klass.url(this.id)
699710
const verb = "delete"
@@ -741,17 +752,6 @@ export class JSORMBase {
741752
})
742753
}
743754

744-
serializeKey(key: string): string {
745-
if (this.klass.letterCase == "dasherized") {
746-
return dasherize(underscore(key))
747-
}
748-
return underscore(key)
749-
}
750-
751-
deserializeKey(key: string): string {
752-
return camelize(underscore(key), false)
753-
}
754-
755755
private async _handleResponse(
756756
response: JsonapiResponse,
757757
callback: () => void

src/util/validation-errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ValidationErrors {
4343
let attribute = meta.attribute
4444

4545
if (this.model.klass.camelizeKeys) {
46-
attribute = this.model.deserializeKey(attribute)
46+
attribute = this.model.klass.deserializeKey(attribute)
4747
}
4848

4949
errorsAccumulator[attribute] = meta.message

src/util/write-payload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class WritePayload<T extends JSORMBase> {
3333

3434
this._eachAttribute((key, value) => {
3535
if (!this.model.isPersisted || this.model.changes()[key]) {
36-
attrs[this.model.serializeKey(key)] = value
36+
attrs[this.model.klass.serializeKey(key)] = value
3737
}
3838
})
3939

@@ -120,7 +120,7 @@ export class WritePayload<T extends JSORMBase> {
120120
}
121121

122122
if (data) {
123-
_relationships[this.model.serializeKey(key)] = { data }
123+
_relationships[this.model.klass.serializeKey(key)] = { data }
124124
}
125125
}
126126
})

0 commit comments

Comments
 (0)