Comparing version 2.1.0 to 2.2.0
CHANGELOG | ||
=== | ||
2.2.0 | ||
-- | ||
New Features | ||
- | ||
Fixed shallow copying on array. Now shallow copy can be used for arrays. | ||
2.1.0 | ||
@@ -4,0 +12,0 @@ -- |
{ | ||
"name": "fun-model", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "fun-model is pure functional implementation of FLUX architecture.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -15,41 +15,64 @@ "use strict"; | ||
}); | ||
it('copies all values', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState.id).toBe(aState.id); | ||
expect(newState.subObject.id).toBe(aState.subObject.id); | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
it('returns new object', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState).not.toBe(aState); | ||
}); | ||
it('returns original sub objects', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState.subObject).toBe(aState.subObject); | ||
expect(newState.list).toBe(aState.list); | ||
}); | ||
it('sets properties in callback without return', function () { | ||
var newState = h.shallowCopy(aState, function (s) { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
describe('on object', function () { | ||
it('copies all values', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState.id).toBe(aState.id); | ||
expect(newState.subObject.id).toBe(aState.subObject.id); | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
it('returns new object', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState).not.toBe(aState); | ||
}); | ||
it('returns original sub objects', function () { | ||
var newState = h.shallowCopy(aState); | ||
expect(newState.subObject).toBe(aState.subObject); | ||
expect(newState.list).toBe(aState.list); | ||
}); | ||
it('sets properties in callback without return', function () { | ||
var newState = h.shallowCopy(aState, function (s) { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
it('sets properties in nested shallowCopy', function () { | ||
var newState = h.shallowCopy(aState, function (s) { return h.shallowCopy(s, function (a) { | ||
a.id = 'newId'; | ||
a.subObject = { id: 'newSubId' }; | ||
return a; | ||
}); }); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
it('sets properties in inline style', function () { | ||
var newState = h.shallowCopy(aState, function (s) { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
}); | ||
it('sets properties in nested shallowCopy', function () { | ||
var newState = h.shallowCopy(aState, function (s) { return h.shallowCopy(s, function (a) { | ||
a.id = 'newId'; | ||
a.subObject = { id: 'newSubId' }; | ||
return a; | ||
}); }); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
it('sets properties in inline style', function () { | ||
var newState = h.shallowCopy(aState, function (s) { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
describe('on array', function () { | ||
var newState; | ||
beforeEach(function () { | ||
newState = h.shallowCopy(aState, function (ns) { | ||
ns.list = h.shallowCopy(ns.list); | ||
}); | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
it('creates instance of array', function () { | ||
expect(Array.isArray(newState.list)).toBeTruthy(); | ||
}); | ||
it('has same length', function () { | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
it('has all own properties/values', function () { | ||
for (var key in newState.list) { | ||
if (newState.list.hasOwnProperty(key) && typeof key !== 'function') { | ||
expect(newState.list[key]).toBe(aState.list[key]); | ||
} | ||
} | ||
}); | ||
}); | ||
@@ -56,0 +79,0 @@ }); |
@@ -17,52 +17,80 @@ import * as h from '../src/helpers'; | ||
it('copies all values', () => { | ||
const newState = h.shallowCopy(aState); | ||
describe('on object', () => { | ||
it('copies all values', () => { | ||
let newState = h.shallowCopy(aState); | ||
expect(newState.id).toBe(aState.id); | ||
expect(newState.subObject.id).toBe(aState.subObject.id); | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
expect(newState.id).toBe(aState.id); | ||
expect(newState.subObject.id).toBe(aState.subObject.id); | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
it('returns new object', () => { | ||
const newState = h.shallowCopy(aState); | ||
it('returns new object', () => { | ||
let newState = h.shallowCopy(aState); | ||
expect(newState).not.toBe(aState); | ||
}); | ||
expect(newState).not.toBe(aState); | ||
}); | ||
it('returns original sub objects', () => { | ||
const newState = h.shallowCopy(aState); | ||
it('returns original sub objects', () => { | ||
let newState = h.shallowCopy(aState); | ||
expect(newState.subObject).toBe(aState.subObject); | ||
expect(newState.list).toBe(aState.list); | ||
}); | ||
expect(newState.subObject).toBe(aState.subObject); | ||
expect(newState.list).toBe(aState.list); | ||
}); | ||
it('sets properties in callback without return', () => { | ||
const newState = h.shallowCopy(aState, s => { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
it('sets properties in callback without return', () => { | ||
let newState = h.shallowCopy(aState, s => { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
it('sets properties in nested shallowCopy', () => { | ||
let newState = h.shallowCopy(aState, s => h.shallowCopy(s, a => { | ||
a.id = 'newId'; | ||
a.subObject = { id: 'newSubId' }; | ||
return a; | ||
})); | ||
it('sets properties in nested shallowCopy', () => { | ||
const newState = h.shallowCopy(aState, s => h.shallowCopy(s, a => { | ||
a.id = 'newId'; | ||
a.subObject = { id: 'newSubId' }; | ||
return a; | ||
})); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
it('sets properties in inline style', () => { | ||
let newState = h.shallowCopy(aState, s => { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
}); | ||
}); | ||
it('sets properties in inline style', () => { | ||
const newState = h.shallowCopy(aState, s => { | ||
s.id = 'newId'; | ||
s.subObject = { id: 'newSubId' }; | ||
describe('on array', () => { | ||
let newState: IDummyState; | ||
beforeEach(() => { | ||
newState = h.shallowCopy(aState, ns => { | ||
ns.list = h.shallowCopy(ns.list); | ||
}); | ||
}); | ||
expect(newState.id).toBe('newId'); | ||
expect(newState.subObject).toEqual({ id: 'newSubId' }); | ||
it('creates instance of array', () => { | ||
expect(Array.isArray(newState.list)).toBeTruthy(); | ||
}); | ||
it('has same length', () => { | ||
expect(newState.list.length).toBe(aState.list.length); | ||
}); | ||
it('has all own properties/values', () => { | ||
for (let key in newState.list) { | ||
if (newState.list.hasOwnProperty(key) && typeof key !== 'function') { | ||
expect(newState.list[key]).toBe(aState.list[key]); | ||
} | ||
} | ||
}) | ||
}); | ||
@@ -69,0 +97,0 @@ }); |
"use strict"; | ||
function shallowCopy(source, callback) { | ||
if (source instanceof Array) { | ||
source = source.slice(); | ||
var result = callback ? callback(source) : undefined; | ||
return result || source; | ||
} | ||
return objectShallowCopy(source, callback); | ||
} | ||
exports.shallowCopy = shallowCopy; | ||
function objectShallowCopy(source, callback) { | ||
if (callback === void 0) { callback = function (t) { }; } | ||
@@ -11,4 +20,12 @@ var target = {}; | ||
} | ||
exports.shallowCopy = shallowCopy; | ||
exports.objectShallowCopy = objectShallowCopy; | ||
; | ||
// function objectShallowCopy<T>(source: T, callback?: (target: T) => void | T): T { | ||
// const target = <T>{}; | ||
// for (var property in source) | ||
// if (source.hasOwnProperty(property)) | ||
// target[property] = source[property]; | ||
// const result = callback ? callback(source) : undefined; | ||
// return <T>result || target; | ||
// }; | ||
function deepFreeze(o) { | ||
@@ -15,0 +32,0 @@ Object.freeze(o); |
@@ -1,2 +0,14 @@ | ||
export function shallowCopy<T>(source: T, callback: (target: T) => void | T = (t: T) => { }): T { | ||
export function shallowCopy<T>(source: T, callback?: (target: T) => void | T): T; | ||
export function shallowCopy<T>(source: T[], callback?: (target: T[]) => void | T[]): T[]; | ||
export function shallowCopy(source: any, callback?: (target: any) => void | any): any { | ||
if (source instanceof Array) { | ||
source = [...source]; | ||
const result = callback ? callback(source) : undefined; | ||
return result || source; | ||
} | ||
return objectShallowCopy(source, callback); | ||
} | ||
export function objectShallowCopy<T>(source: T, callback: (target: T) => void | T = (t: T) => { }): T { | ||
const target = <T>{}; | ||
@@ -10,6 +22,13 @@ for (var property in source) | ||
// function objectShallowCopy<T>(source: T, callback?: (target: T) => void | T): T { | ||
// const target = <T>{}; | ||
// for (var property in source) | ||
// if (source.hasOwnProperty(property)) | ||
// target[property] = source[property]; | ||
// const result = callback ? callback(source) : undefined; | ||
// return <T>result || target; | ||
// }; | ||
export function deepFreeze(o) { | ||
Object.freeze(o); | ||
Object.getOwnPropertyNames(o).forEach(function (prop) { | ||
@@ -23,4 +42,3 @@ if (o.hasOwnProperty(prop) | ||
}); | ||
return o; | ||
}; |
233030
1656