react-hooks-library
Advanced tools
Comparing version 0.0.10 to 0.0.11
import { Numeric } from './Numeric'; | ||
export declare class Currency extends Numeric { | ||
readonly presentation: string; | ||
readonly yuan: number; | ||
format: (formatString?: string | undefined) => string; | ||
} |
@@ -13,2 +13,9 @@ import * as tslib_1 from "tslib"; | ||
} | ||
Object.defineProperty(Currency.prototype, "presentation", { | ||
get: function () { | ||
return this.format(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Currency.prototype, "yuan", { | ||
@@ -15,0 +22,0 @@ get: function () { |
@@ -6,4 +6,5 @@ import { Moment } from 'moment'; | ||
moment: Moment; | ||
readonly presentation: string; | ||
constructor(value: string, field: IFieldExcludeKind<IDateField>); | ||
format: (formatString?: string) => string; | ||
} |
@@ -15,4 +15,11 @@ import * as tslib_1 from "tslib"; | ||
} | ||
Object.defineProperty(Date.prototype, "presentation", { | ||
get: function () { | ||
return this.format(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return Date; | ||
}(Value)); | ||
export { Date }; |
@@ -6,3 +6,3 @@ import { isObject, isArray, isNumber, isString, isRegExp } from 'lodash'; | ||
import { Numeric } from './Numeric'; | ||
import { Presentation } from './Presentation'; | ||
import { Text } from './Text'; | ||
export function decorateModel(data, defintions) { | ||
@@ -38,3 +38,2 @@ if (isObject(data)) { | ||
displayName: defintion.displayName, | ||
chartType: defintion.chartType, | ||
decimals: defintion.decimals, | ||
@@ -46,8 +45,7 @@ isPercent: defintion.isPercent, | ||
displayName: defintion.displayName, | ||
chartType: defintion.chartType, | ||
decimals: defintion.decimals, | ||
isPercent: defintion.isPercent, | ||
}); | ||
case FieldKind.Presentation: | ||
return new Presentation(value, { | ||
case FieldKind.Text: | ||
return new Text(value, { | ||
displayName: defintion.displayName, | ||
@@ -54,0 +52,0 @@ valueMapper: defintion.valueMapper, |
@@ -5,4 +5,4 @@ export * from './Numeric'; | ||
export * from './Field'; | ||
export * from './Presentation'; | ||
export * from './Text'; | ||
export * from './Value'; | ||
export * from './decorateModel'; |
@@ -5,4 +5,4 @@ export * from './Numeric'; | ||
export * from './Field'; | ||
export * from './Presentation'; | ||
export * from './Text'; | ||
export * from './Value'; | ||
export * from './decorateModel'; |
/// <reference types="numeral" /> | ||
import { INumericField, ChartType, IFieldExcludeKind } from './types'; | ||
import { INumericField, IFieldExcludeKind } from './types'; | ||
import { Value } from './Value'; | ||
@@ -7,8 +7,9 @@ export declare class Numeric extends Value<number> { | ||
decimals: number; | ||
chartType?: ChartType; | ||
isPercent?: boolean; | ||
protected readonly defaultFormatString: string; | ||
readonly presentation: string; | ||
value: number; | ||
protected readonly defaultFormatString: string; | ||
constructor(value: number, numericField: IFieldExcludeKind<INumericField>); | ||
calcTrend: (compare: number) => number | undefined; | ||
format: (formatString?: string | undefined) => string; | ||
} |
@@ -8,2 +8,11 @@ import * as tslib_1 from "tslib"; | ||
var _this = _super.call(this, value, numericField) || this; | ||
_this.calcTrend = function (compare) { | ||
if (!compare || compare === 0) { | ||
return undefined; | ||
} | ||
return numeral(_this.value) | ||
.subtract(compare) | ||
.divide(compare) | ||
.value(); | ||
}; | ||
_this.format = function (formatString) { | ||
@@ -14,12 +23,18 @@ return _this.numeral.format(formatString || _this.defaultFormatString); | ||
_this.decimals = numericField.decimals; | ||
_this.chartType = numericField.chartType; | ||
_this.isPercent = numericField.isPercent; | ||
return _this; | ||
} | ||
Object.defineProperty(Numeric.prototype, "value", { | ||
Object.defineProperty(Numeric.prototype, "defaultFormatString", { | ||
get: function () { | ||
return this.numeral.value(); | ||
if (this.decimals > 0) { | ||
return "0,0." + '0'.repeat(this.decimals); | ||
} | ||
return '0,0'; | ||
}, | ||
set: function (newValue) { | ||
this.numeral = numeral(newValue); | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Numeric.prototype, "presentation", { | ||
get: function () { | ||
return this.format(); | ||
}, | ||
@@ -29,9 +44,9 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(Numeric.prototype, "defaultFormatString", { | ||
Object.defineProperty(Numeric.prototype, "value", { | ||
get: function () { | ||
if (this.decimals > 0) { | ||
return "0,0." + '0'.repeat(this.decimals); | ||
} | ||
return '0,0'; | ||
return this.numeral.value(); | ||
}, | ||
set: function (newValue) { | ||
this.numeral = numeral(newValue); | ||
}, | ||
enumerable: true, | ||
@@ -38,0 +53,0 @@ configurable: true |
export declare enum FieldKind { | ||
Presentation = 1, | ||
Text = 1, | ||
Date = 2, | ||
@@ -7,6 +7,2 @@ Numeric = 10, | ||
} | ||
export declare enum ChartType { | ||
LINE = 0, | ||
BAR = 1 | ||
} | ||
export interface IField { | ||
@@ -21,3 +17,2 @@ kind: FieldKind; | ||
isPercent?: boolean; | ||
chartType?: ChartType; | ||
} | ||
@@ -28,6 +23,5 @@ export interface ICurrencyField extends IField { | ||
isPercent?: boolean; | ||
chartType?: ChartType; | ||
} | ||
export interface IPresentationField extends IField { | ||
kind: FieldKind.Presentation; | ||
kind: FieldKind.Text; | ||
valueMapper?: { | ||
@@ -34,0 +28,0 @@ [key: string]: string; |
export var FieldKind; | ||
(function (FieldKind) { | ||
// 展示 | ||
FieldKind[FieldKind["Presentation"] = 1] = "Presentation"; | ||
FieldKind[FieldKind["Text"] = 1] = "Text"; | ||
// 日期 | ||
@@ -12,6 +12,1 @@ FieldKind[FieldKind["Date"] = 2] = "Date"; | ||
})(FieldKind || (FieldKind = {})); | ||
export var ChartType; | ||
(function (ChartType) { | ||
ChartType[ChartType["LINE"] = 0] = "LINE"; | ||
ChartType[ChartType["BAR"] = 1] = "BAR"; | ||
})(ChartType || (ChartType = {})); |
import { IFieldExcludeKind, IField } from './types'; | ||
import { Field } from './Field'; | ||
export declare class Value<P> { | ||
export declare abstract class Value<P> { | ||
value: P; | ||
field: Field; | ||
abstract readonly presentation: string; | ||
constructor(value: P, field: IFieldExcludeKind<IField>); | ||
} |
@@ -5,3 +5,2 @@ import * as tslib_1 from "tslib"; | ||
import { usePrevious } from './usePrevious'; | ||
import { decorateModel } from '../fields'; | ||
export function useQuery(props) { | ||
@@ -51,14 +50,8 @@ var _this = this; | ||
if (response) { | ||
if (response['defintions']) { | ||
setData(tslib_1.__assign({}, response, { data: decorateModel(response['data'], response['defintions']) })); | ||
} | ||
else { | ||
setData(response); | ||
} | ||
setLoading(false); | ||
setData(response); | ||
} | ||
else { | ||
reset(); | ||
setLoading(false); | ||
} | ||
setLoading(false); | ||
return [2 /*return*/, response]; | ||
@@ -65,0 +58,0 @@ } |
{ | ||
"name": "react-hooks-library", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -6,2 +6,6 @@ import numeral from 'numeral' | ||
export class Currency extends Numeric { | ||
public get presentation() { | ||
return this.format() | ||
} | ||
public get yuan(): number { | ||
@@ -8,0 +12,0 @@ return numeral(this.numeral) |
@@ -9,2 +9,6 @@ import moment, { Moment } from 'moment' | ||
public get presentation() { | ||
return this.format() | ||
} | ||
constructor(value: string, field: IFieldExcludeKind<IDateField>) { | ||
@@ -11,0 +15,0 @@ super(value, field) |
@@ -13,3 +13,3 @@ import { isObject, isArray, isNumber, isString, isRegExp } from 'lodash' | ||
import { Numeric } from './Numeric' | ||
import { Presentation } from './Presentation' | ||
import { Text } from './Text' | ||
@@ -59,3 +59,2 @@ export interface IDefintion { | ||
displayName: defintion.displayName, | ||
chartType: defintion.chartType, | ||
decimals: defintion.decimals, | ||
@@ -67,8 +66,7 @@ isPercent: defintion.isPercent, | ||
displayName: defintion.displayName, | ||
chartType: defintion.chartType, | ||
decimals: defintion.decimals, | ||
isPercent: defintion.isPercent, | ||
}) | ||
case FieldKind.Presentation: | ||
return new Presentation(value, { | ||
case FieldKind.Text: | ||
return new Text(value, { | ||
displayName: defintion.displayName, | ||
@@ -75,0 +73,0 @@ valueMapper: defintion.valueMapper, |
@@ -5,4 +5,4 @@ export * from './Numeric' | ||
export * from './Field' | ||
export * from './Presentation' | ||
export * from './Text' | ||
export * from './Value' | ||
export * from './decorateModel' |
import numeral from 'numeral' | ||
import { INumericField, ChartType, IFieldExcludeKind } from './types' | ||
import { INumericField, IFieldExcludeKind } from './types' | ||
import { Value } from './Value' | ||
@@ -9,5 +9,16 @@ | ||
public decimals: number | ||
public chartType?: ChartType | ||
public isPercent?: boolean | ||
protected get defaultFormatString(): string { | ||
if (this.decimals > 0) { | ||
return `0,0.${'0'.repeat(this.decimals)}` | ||
} | ||
return '0,0' | ||
} | ||
public get presentation() { | ||
return this.format() | ||
} | ||
public get value(): number { | ||
@@ -21,10 +32,2 @@ return this.numeral.value() | ||
protected get defaultFormatString(): string { | ||
if (this.decimals > 0) { | ||
return `0,0.${'0'.repeat(this.decimals)}` | ||
} | ||
return '0,0' | ||
} | ||
constructor(value: number, numericField: IFieldExcludeKind<INumericField>) { | ||
@@ -35,6 +38,16 @@ super(value, numericField) | ||
this.decimals = numericField.decimals | ||
this.chartType = numericField.chartType | ||
this.isPercent = numericField.isPercent | ||
} | ||
public calcTrend = (compare: number) => { | ||
if (!compare || compare === 0) { | ||
return undefined | ||
} | ||
return numeral(this.value) | ||
.subtract(compare) | ||
.divide(compare) | ||
.value() | ||
} | ||
public format = (formatString?: string) => { | ||
@@ -41,0 +54,0 @@ return this.numeral.format(formatString || this.defaultFormatString) |
export enum FieldKind { | ||
// 展示 | ||
Presentation = 1, | ||
Text = 1, | ||
// 日期 | ||
@@ -12,7 +12,2 @@ Date = 2, | ||
export enum ChartType { | ||
LINE, | ||
BAR, | ||
} | ||
export interface IField { | ||
@@ -29,3 +24,2 @@ kind: FieldKind | ||
isPercent?: boolean | ||
chartType?: ChartType | ||
} | ||
@@ -37,7 +31,6 @@ | ||
isPercent?: boolean | ||
chartType?: ChartType | ||
} | ||
export interface IPresentationField extends IField { | ||
kind: FieldKind.Presentation | ||
kind: FieldKind.Text | ||
valueMapper?: { [key: string]: string } | ||
@@ -44,0 +37,0 @@ } |
import { IFieldExcludeKind, IField } from './types' | ||
import { Field } from './Field' | ||
export class Value<P> { | ||
export abstract class Value<P> { | ||
public value: P | ||
public field: Field | ||
public abstract get presentation(): string | ||
constructor(value: P, field: IFieldExcludeKind<IField>) { | ||
@@ -9,0 +11,0 @@ this.value = value |
@@ -5,3 +5,2 @@ import { useState, useEffect } from 'react' | ||
import { usePrevious } from './usePrevious' | ||
import { decorateModel } from '../fields' | ||
@@ -61,18 +60,9 @@ export interface IQueryProps<P> { | ||
if (response) { | ||
if (response['defintions']) { | ||
setData({ | ||
...response, | ||
data: decorateModel(response['data'], response['defintions']), | ||
}) | ||
} else { | ||
setData(response) | ||
} | ||
setLoading(false) | ||
setData(response) | ||
} else { | ||
reset() | ||
setLoading(false) | ||
} | ||
setLoading(false) | ||
return response | ||
@@ -79,0 +69,0 @@ } catch (error) { |
import { Currency } from '../../src/fields' | ||
import { ChartType } from '../../src/fields/types' | ||
@@ -7,3 +6,2 @@ describe('Model Currency', () => { | ||
decimals: 2, | ||
chartType: ChartType.BAR, | ||
isPercent: false, | ||
@@ -10,0 +8,0 @@ displayName: '测试', |
import { Numeric } from '../../src/fields' | ||
import { ChartType } from '../../src/fields/types' | ||
@@ -7,3 +6,2 @@ describe('Model Numeric', () => { | ||
decimals: 2, | ||
chartType: ChartType.BAR, | ||
isPercent: false, | ||
@@ -10,0 +8,0 @@ displayName: '测试', |
@@ -1,5 +0,5 @@ | ||
import { Presentation } from '../../src/fields' | ||
import { Text } from '../../src/fields' | ||
describe('Model Presentation', () => { | ||
const presentation = new Presentation('test', { | ||
const text = new Text('test', { | ||
displayName: '测试', | ||
@@ -9,14 +9,14 @@ }) | ||
it('should create instance', () => { | ||
expect(JSON.stringify(presentation)).toMatchSnapshot() | ||
expect(JSON.stringify(text)).toMatchSnapshot() | ||
}) | ||
it('should have valueDisplayName', () => { | ||
expect(presentation.valueDisplayName).toBe('test') | ||
expect(text.valueDisplayName).toBe('test') | ||
presentation.valueMapper = { | ||
text.valueMapper = { | ||
test: '测试', | ||
} | ||
expect(presentation.valueDisplayName).toBe('测试') | ||
expect(text.valueDisplayName).toBe('测试') | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
303151
2012