Comparing version 1.3.0 to 1.4.0
@@ -110,5 +110,6 @@ export interface IModelPropertiesMapper { | ||
export declare type SwitchCaseModelMapperOptionsType = { | ||
kebabizeAttributes?: boolean; | ||
kebabizeRelationships?: boolean; | ||
kebabizeType?: boolean; | ||
switchAttributes?: boolean; | ||
switchRelationships?: boolean; | ||
switchType?: boolean; | ||
switchChar?: string; | ||
}; | ||
@@ -119,2 +120,3 @@ export declare type SwitchCaseJsonMapperOptionsType = { | ||
camelizeType?: boolean; | ||
switchChar?: string; | ||
}; |
import { IModelPropertiesMapper, IJsonPropertiesMapper, TAnyKeyValueObject, TJsonaModel, TJsonaRelationships, SwitchCaseModelMapperOptionsType, SwitchCaseJsonMapperOptionsType } from './JsonaTypes'; | ||
import { ModelPropertiesMapper, JsonPropertiesMapper } from './'; | ||
export declare class SwitchCaseModelMapper extends ModelPropertiesMapper implements IModelPropertiesMapper { | ||
kebabizeAttributes: boolean; | ||
kebabizeRelationships: boolean; | ||
kebabizeType: boolean; | ||
switchAttributes: boolean; | ||
switchRelationships: boolean; | ||
switchType: boolean; | ||
switchChar: string; | ||
constructor(options?: SwitchCaseModelMapperOptionsType); | ||
@@ -16,2 +17,3 @@ getType(model: TJsonaModel): any; | ||
camelizeType: boolean; | ||
switchChar: string; | ||
constructor(options?: SwitchCaseJsonMapperOptionsType); | ||
@@ -18,0 +20,0 @@ createModel(type: string): TJsonaModel; |
@@ -19,18 +19,21 @@ "use strict"; | ||
var _this = _super.call(this) || this; | ||
var _a = options || {}, _b = _a.kebabizeAttributes, kebabizeAttributes = _b === void 0 ? true : _b, _c = _a.kebabizeRelationships, kebabizeRelationships = _c === void 0 ? true : _c, _d = _a.kebabizeType, kebabizeType = _d === void 0 ? true : _d; | ||
_this.kebabizeAttributes = kebabizeAttributes; | ||
_this.kebabizeRelationships = kebabizeRelationships; | ||
_this.kebabizeType = kebabizeType; | ||
var _a = options || {}, _b = _a.switchAttributes, switchAttributes = _b === void 0 ? true : _b, _c = _a.switchRelationships, switchRelationships = _c === void 0 ? true : _c, _d = _a.switchType, switchType = _d === void 0 ? true : _d, _e = _a.switchChar, switchChar = _e === void 0 ? '-' : _e; | ||
_this.switchAttributes = switchAttributes; | ||
_this.switchRelationships = switchRelationships; | ||
_this.switchType = switchType; | ||
_this.switchChar = switchChar; | ||
return _this; | ||
} | ||
SwitchCaseModelMapper.prototype.getType = function (model) { | ||
var _this = this; | ||
var type = _super.prototype.getType.call(this, model); | ||
if (!this.kebabizeType || !type) { | ||
if (!this.switchType || !type) { | ||
return type; | ||
} | ||
return type.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase(); }); | ||
return type.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
}; | ||
SwitchCaseModelMapper.prototype.getAttributes = function (model) { | ||
var _this = this; | ||
var camelCasedAttributes = _super.prototype.getAttributes.call(this, model); | ||
if (!this.kebabizeAttributes || !camelCasedAttributes) { | ||
if (!this.switchAttributes || !camelCasedAttributes) { | ||
return camelCasedAttributes; | ||
@@ -40,3 +43,3 @@ } | ||
Object.keys(camelCasedAttributes).forEach(function (name) { | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase(); }); | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
kebabAttributes[kebabName] = camelCasedAttributes[name]; | ||
@@ -47,4 +50,5 @@ }); | ||
SwitchCaseModelMapper.prototype.getRelationships = function (model) { | ||
var _this = this; | ||
var camelCasedRelationships = _super.prototype.getRelationships.call(this, model); | ||
if (!this.kebabizeRelationships || !camelCasedRelationships) { | ||
if (!this.switchRelationships || !camelCasedRelationships) { | ||
return camelCasedRelationships; | ||
@@ -54,3 +58,3 @@ } | ||
Object.keys(camelCasedRelationships).forEach(function (name) { | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase(); }); | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
kebabRelationships[kebabName] = camelCasedRelationships[name]; | ||
@@ -67,6 +71,7 @@ }); | ||
var _this = _super.call(this) || this; | ||
var _a = options || {}, _b = _a.camelizeAttributes, camelizeAttributes = _b === void 0 ? true : _b, _c = _a.camelizeRelationships, camelizeRelationships = _c === void 0 ? true : _c, _d = _a.camelizeType, camelizeType = _d === void 0 ? true : _d; | ||
var _a = options || {}, _b = _a.camelizeAttributes, camelizeAttributes = _b === void 0 ? true : _b, _c = _a.camelizeRelationships, camelizeRelationships = _c === void 0 ? true : _c, _d = _a.camelizeType, camelizeType = _d === void 0 ? true : _d, _e = _a.switchChar, switchChar = _e === void 0 ? '-' : _e; | ||
_this.camelizeAttributes = camelizeAttributes; | ||
_this.camelizeRelationships = camelizeRelationships; | ||
_this.camelizeType = camelizeType; | ||
_this.switchChar = switchChar; | ||
return _this; | ||
@@ -78,6 +83,8 @@ } | ||
} | ||
var camelizedType = type.replace(/-([a-z0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
var regex = new RegExp(this.switchChar + "([a-z0-9])", 'g'); | ||
var camelizedType = type.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
return { type: camelizedType }; | ||
}; | ||
SwitchCaseJsonMapper.prototype.setAttributes = function (model, attributes) { | ||
var _this = this; | ||
if (!this.camelizeAttributes) { | ||
@@ -87,3 +94,4 @@ return _super.prototype.setAttributes.call(this, model, attributes); | ||
Object.keys(attributes).forEach(function (propName) { | ||
var camelName = propName.replace(/-([a-z0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
var regex = new RegExp(_this.switchChar + "([a-z0-9])", 'g'); | ||
var camelName = propName.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
model[camelName] = attributes[propName]; | ||
@@ -93,2 +101,3 @@ }); | ||
SwitchCaseJsonMapper.prototype.setRelationships = function (model, relationships) { | ||
var _this = this; | ||
// call super.setRelationships first, just for not to copy paste setRelationships logic | ||
@@ -101,3 +110,4 @@ _super.prototype.setRelationships.call(this, model, relationships); | ||
model[simplePropertyMappers_1.RELATIONSHIP_NAMES_PROP].forEach(function (kebabName, i) { | ||
var camelName = kebabName.replace(/-([a-z]|[0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
var regex = new RegExp(_this.switchChar + "([a-z0-9])", 'g'); | ||
var camelName = kebabName.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
if (camelName !== kebabName) { | ||
@@ -104,0 +114,0 @@ model[camelName] = model[kebabName]; |
{ | ||
"name": "jsona", | ||
"description": "Provide data formatters (data model builder & json builder) to work with JSON API specification v1.0 in your JavaScript / TypeScript code", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "json-api", |
@@ -133,5 +133,6 @@ | ||
export type SwitchCaseModelMapperOptionsType = { | ||
kebabizeAttributes?: boolean, | ||
kebabizeRelationships?: boolean, | ||
kebabizeType?: boolean, | ||
switchAttributes?: boolean, | ||
switchRelationships?: boolean, | ||
switchType?: boolean, | ||
switchChar?: string, | ||
}; | ||
@@ -142,2 +143,3 @@ export type SwitchCaseJsonMapperOptionsType = { | ||
camelizeType?: boolean, | ||
}; | ||
switchChar?: string, | ||
}; |
@@ -14,5 +14,6 @@ import { | ||
kebabizeAttributes: boolean; | ||
kebabizeRelationships: boolean; | ||
kebabizeType: boolean; | ||
switchAttributes: boolean; | ||
switchRelationships: boolean; | ||
switchType: boolean; | ||
switchChar: string; | ||
@@ -23,10 +24,12 @@ constructor(options?: SwitchCaseModelMapperOptionsType) { | ||
const { | ||
kebabizeAttributes = true, | ||
kebabizeRelationships = true, | ||
kebabizeType = true, | ||
switchAttributes = true, | ||
switchRelationships = true, | ||
switchType = true, | ||
switchChar = '-', | ||
} = options || {}; | ||
this.kebabizeAttributes = kebabizeAttributes; | ||
this.kebabizeRelationships = kebabizeRelationships; | ||
this.kebabizeType = kebabizeType; | ||
this.switchAttributes = switchAttributes; | ||
this.switchRelationships = switchRelationships; | ||
this.switchType = switchType; | ||
this.switchChar = switchChar; | ||
} | ||
@@ -37,7 +40,7 @@ | ||
if (!this.kebabizeType || !type) { | ||
if (!this.switchType || !type) { | ||
return type; | ||
} | ||
return type.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase() }); | ||
return type.replace(/([a-z][A-Z0-9])/g, g => g[0] + this.switchChar + g[1].toLowerCase()); | ||
} | ||
@@ -48,3 +51,3 @@ | ||
if (!this.kebabizeAttributes || !camelCasedAttributes) { | ||
if (!this.switchAttributes || !camelCasedAttributes) { | ||
return camelCasedAttributes; | ||
@@ -55,3 +58,3 @@ } | ||
Object.keys(camelCasedAttributes).forEach((name) => { | ||
const kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase() }); | ||
const kebabName = name.replace(/([a-z][A-Z0-9])/g, g => g[0] + this.switchChar + g[1].toLowerCase()); | ||
kebabAttributes[kebabName] = camelCasedAttributes[name]; | ||
@@ -65,3 +68,3 @@ }); | ||
if (!this.kebabizeRelationships || !camelCasedRelationships) { | ||
if (!this.switchRelationships || !camelCasedRelationships) { | ||
return camelCasedRelationships; | ||
@@ -72,3 +75,3 @@ } | ||
Object.keys(camelCasedRelationships).forEach((name) => { | ||
const kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + '-' + g[1].toLowerCase() }); | ||
const kebabName = name.replace(/([a-z][A-Z0-9])/g, g => g[0] + this.switchChar + g[1].toLowerCase()); | ||
kebabRelationships[kebabName] = camelCasedRelationships[name]; | ||
@@ -85,2 +88,3 @@ }); | ||
camelizeType: boolean; | ||
switchChar: string; | ||
@@ -94,2 +98,3 @@ constructor(options?: SwitchCaseJsonMapperOptionsType) { | ||
camelizeType = true, | ||
switchChar = '-' | ||
} = options || {}; | ||
@@ -100,2 +105,3 @@ | ||
this.camelizeType = camelizeType; | ||
this.switchChar = switchChar; | ||
} | ||
@@ -108,3 +114,4 @@ | ||
const camelizedType = type.replace(/-([a-z0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
const regex = new RegExp(`${this.switchChar}([a-z0-9])`, 'g'); | ||
const camelizedType = type.replace(regex, g => g[1].toUpperCase()); | ||
return {type: camelizedType}; | ||
@@ -119,3 +126,4 @@ } | ||
Object.keys(attributes).forEach((propName) => { | ||
const camelName = propName.replace(/-([a-z0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
const regex = new RegExp(`${this.switchChar}([a-z0-9])`, 'g'); | ||
const camelName = propName.replace(regex, g => g[1].toUpperCase()); | ||
model[camelName] = attributes[propName]; | ||
@@ -135,3 +143,4 @@ }); | ||
model[RELATIONSHIP_NAMES_PROP].forEach((kebabName, i) => { | ||
const camelName = kebabName.replace(/-([a-z]|[0-9])/g, function (g) { return g[1].toUpperCase(); }); | ||
const regex = new RegExp(`${this.switchChar}([a-z0-9])`, 'g'); | ||
const camelName = kebabName.replace(regex, g => g[1].toUpperCase()); | ||
if (camelName !== kebabName) { | ||
@@ -138,0 +147,0 @@ model[camelName] = model[kebabName]; |
Sorry, the diff of this file is not supported yet
124750
2127