gpt4 book ai didi

javascript - 如何使用 facebook/immutable-js 制作自定义不可变类型?

转载 作者:行者123 更新时间:2023-11-30 16:41:59 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的不可变 Interval 类型。它有一个开始和一个结束属性。有没有一种直接的方法来利用 immutable-js库来创建您自己的自定义不可变类型?

我目前对 Immutable.Map 和 Immutable.List 使用包含模式的尝试效果不佳。例如,difference 应该返回一个不可变的 Interval 列表,但是因为它创建了 Interval 的新实例,所以它返回的 List 不会通过相等性。

感觉我采取了错误的方法,但我不确定如何。我可以很容易地用 Immutable.Map 表示数据,但我想在其公共(public)接口(interface)中包含 difference,而不包含一般的 Map 方法,例如获取和设置。即我希望它是不可变的,但它是具有自己的接口(interface)的自己的类型。

var Immutable = require('immutable')

function Interval(start, end) {
this.value = Immutable.Map({
start: start,
end: end
})
}

Interval.prototype.start = function () {
return this.value.get('start')
}

Interval.prototype.end = function () {
return this.value.get('end')
}

Interval.prototype.equals = function(interval) {
return this.value.equals(interval.value)
}

Interval.prototype.difference = function (subtrahend) {
var nonoverlapping = subtrahend.start() > this.end() || subtrahend.end() < this.start()

return nonoverlapping ? Immutable.List.of(this) :
Immutable.List.of(
new Interval(this.start(), subtrahend.start()),
new Interval(subtrahend.end(), this.end())
)
.filter(function(interval) {
return interval.end() > interval.start()
})
}

一些测试:

it('should return an empty array for an exact difference', function() {
var interval = new Interval(80, 90)
interval.difference(interval)
.equals(Immutable.List())
.should.be.true
})

it('should return an array with the same Interval for a missed difference', function() {
var interval = new Interval(80, 90)
console.log(interval.difference(new Interval(50, 60)).toJS());
interval.difference(new Interval(50, 60))
.equals(Immutable.List.of([
{ value: Immutable.Map({ start: 80, end: 90 }) }
]))
.should.be.true
})

最佳答案

为什么不使用 Record 类型:

var Interval = Immutable.Record({start: 0, end: 1})

这将为您创建记录类型,定义 startend 属性,默认值分别为 0 和 1。

现在要创建 Interval 的实例,只需执行以下操作:

var newInterval = new Interval() //will have default values above
var newInterval = new Interval({start: 10, end: 30)

请注意,Record 类型的实例只是值类型,因此它们应该只包含数据。当然,您可以定义对该数据值进行操作的函数。

var difference = function (interval1, interval2) {
var nonoverlapping = interval2.start > interval1.end || interval2.end < interval1.start

return nonoverlapping ? Immutable.List.of(interval1) :
Immutable.List([
new Interval({start: interval1.start, end: interval2.start}),
new Interval({start: interval2.end, end: interval1.end})
])
.filter(function(interval) {
return interval.end > interval.start
});
}

使用 Immutable.is(interval1, interval2)

免费获得平等

有关记录类型的更多信息:http://facebook.github.io/immutable-js/docs/#/Record

关于javascript - 如何使用 facebook/immutable-js 制作自定义不可变类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com