Skip to content

Commit

Permalink
feat: deepFreeze is moved to js-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jul 28, 2023
1 parent e483ab1 commit 6b40989
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
13 changes: 1 addition & 12 deletions src/testing/testing.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { CommonLogger } from '@naturalcycles/js-lib'
import { expectTypeOf } from 'expect-type'
import { deepFreeze, jestLog, jestLogger, silentConsole } from './testing.util'

test('deepFreeze', () => {
const o = {
a: {
b: 'bb',
},
}
deepFreeze(o)
expect(() => (o.a = 'cc' as any)).toThrow()
expect(() => (o.a.b = 'cc')).toThrow()
})
import { jestLog, jestLogger, silentConsole } from './testing.util'

test('jestLogger', () => {
jestLog('hello')
Expand Down
20 changes: 0 additions & 20 deletions src/testing/testing.util.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import { commonLoggerCreate } from '@naturalcycles/js-lib'
import { inspectAny } from '@naturalcycles/nodejs-lib'

/**
* Does Object.freeze recursively for given object.
*
* Based on: https://github.com/substack/deep-freeze/blob/master/index.js
*/
export function deepFreeze(o: any): void {
Object.freeze(o)

Object.getOwnPropertyNames(o).forEach(prop => {
if (
o.hasOwnProperty(prop) && // eslint-disable-line no-prototype-builtins
o[prop] !== null &&
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
!Object.isFrozen(o[prop])
) {
deepFreeze(o[prop])
}
})
}

export function silentConsole(): void {
console.log = () => {}
console.debug = () => {}
Expand Down
78 changes: 60 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,9 @@
typescript "^5.0.2"

"@naturalcycles/js-lib@^14.0.0":
version "14.159.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-14.159.0.tgz#90b5f7af1e316071c2c09a0095aed298c2be1c76"
integrity sha512-54TpEwsTJUtOOqVxAbIYzGtb198YELK58jjhJl7YKlJ8ApZV9mFmjEHg3/X1kpiMHJHzZBZYoKA+kAd5komEuQ==
version "14.161.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-14.161.0.tgz#49f46fc2622b81407d34cbeb265ac71856f97abc"
integrity sha512-uuQjUb4hOpSr7LTSxsIDvveKqLQgwqhbKjDHo4u8EbufVaGKTO/60LGFu7R6LkqRQsNa9WPcUT7B9x2Q+PlJYQ==
dependencies:
tslib "^2.0.0"
zod "^3.20.2"
Expand Down Expand Up @@ -1431,6 +1431,17 @@ array-union@^2.1.0:
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"
integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==

array.prototype.findlastindex@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/array.prototype.findlastindex/-/array.prototype.findlastindex-1.2.2.tgz#bc229aef98f6bd0533a2bc61ff95209875526c9b"
integrity sha512-tb5thFFlUcp7NdNF6/MpDk/1r/4awWG1FIz3YqDf+/zJSTezBb+/5WViH41obXULHVpDzoiCLpJ/ZO9YbJMsdw==
dependencies:
call-bind "^1.0.2"
define-properties "^1.1.4"
es-abstract "^1.20.4"
es-shim-unscopables "^1.0.0"
get-intrinsic "^1.1.3"

array.prototype.flat@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/array.prototype.flat/-/array.prototype.flat-1.3.1.tgz#ffc6576a7ca3efc2f46a143b9d1dda9b4b3cf5e2"
Expand Down Expand Up @@ -2058,9 +2069,9 @@ ecdsa-sig-formatter@1.0.11:
safe-buffer "^5.0.1"

electron-to-chromium@^1.4.431:
version "1.4.473"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.473.tgz#4853de13a335c70fe1f9df8d4029be54068767d1"
integrity sha512-aVfC8+440vGfl06l8HKKn8/PD5jRfSnLkTTD65EFvU46igbpQRri1gxSzW9/+TeUlwYzrXk1sw867T96zlyECA==
version "1.4.475"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.475.tgz#2fee0e2a70cc1538b94f7f90aabcc436e4dcc827"
integrity sha512-mTye5u5P98kSJO2n7zYALhpJDmoSQejIGya0iR01GpoRady8eK3bw7YHHnjA1Rfi4ZSLdpuzlAC7Zw+1Zu7Z6A==

emittery@^0.13.1:
version "0.13.1"
Expand Down Expand Up @@ -2091,7 +2102,7 @@ error-ex@^1.3.1:
dependencies:
is-arrayish "^0.2.1"

es-abstract@^1.19.0, es-abstract@^1.20.4:
es-abstract@^1.19.0, es-abstract@^1.20.4, es-abstract@^1.21.2:
version "1.22.1"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.22.1.tgz#8b4e5fc5cefd7f1660f0f8e1a52900dfbc9d9ccc"
integrity sha512-ioRRcXMO6OFyRpyzV3kE1IIBd4WG5/kltnzdxSCqoP8CMGs/Li+M1uF5o7lOkZVFjDs+NLesthnF66Pg/0q0Lw==
Expand Down Expand Up @@ -2195,33 +2206,36 @@ eslint-import-resolver-node@^0.3.7:
is-core-module "^2.11.0"
resolve "^1.22.1"

eslint-module-utils@^2.7.4:
eslint-module-utils@^2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.8.0.tgz#e439fee65fc33f6bba630ff621efc38ec0375c49"
integrity sha512-aWajIYfsqCKRDgUfjEXNN/JlrzauMuSEy5sbd7WXbtW3EH6A6MpwEh42c7qD+MqQo9QMJ6fWLAeIJynx0g6OAw==
dependencies:
debug "^3.2.7"

eslint-plugin-import@^2.22.1:
version "2.27.5"
resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.27.5.tgz#876a6d03f52608a3e5bb439c2550588e51dd6c65"
integrity sha512-LmEt3GVofgiGuiE+ORpnvP+kAm3h6MLZJ4Q5HCyHADofsb4VzXFsRiWj3c0OFiV+3DWFh0qg3v9gcPlfc3zRow==
version "2.28.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.28.0.tgz#8d66d6925117b06c4018d491ae84469eb3cb1005"
integrity sha512-B8s/n+ZluN7sxj9eUf7/pRFERX0r5bnFA2dCaLHy2ZeaQEAz0k+ZZkFWRFHJAqxfxQDx6KLv9LeIki7cFdwW+Q==
dependencies:
array-includes "^3.1.6"
array.prototype.findlastindex "^1.2.2"
array.prototype.flat "^1.3.1"
array.prototype.flatmap "^1.3.1"
debug "^3.2.7"
doctrine "^2.1.0"
eslint-import-resolver-node "^0.3.7"
eslint-module-utils "^2.7.4"
eslint-module-utils "^2.8.0"
has "^1.0.3"
is-core-module "^2.11.0"
is-core-module "^2.12.1"
is-glob "^4.0.3"
minimatch "^3.1.2"
object.fromentries "^2.0.6"
object.groupby "^1.0.0"
object.values "^1.1.6"
resolve "^1.22.1"
semver "^6.3.0"
tsconfig-paths "^3.14.1"
resolve "^1.22.3"
semver "^6.3.1"
tsconfig-paths "^3.14.2"

eslint-plugin-jest@^27.0.1:
version "27.2.3"
Expand Down Expand Up @@ -2981,7 +2995,7 @@ is-callable@^1.1.3, is-callable@^1.1.4, is-callable@^1.2.7:
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.7.tgz#3bc2a85ea742d9e36205dcacdd72ca1fdc51b055"
integrity sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==

is-core-module@^2.11.0, is-core-module@^2.5.0:
is-core-module@^2.11.0, is-core-module@^2.12.0, is-core-module@^2.12.1, is-core-module@^2.5.0:
version "2.12.1"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.12.1.tgz#0c0b6885b6f80011c71541ce15c8d66cf5a4f9fd"
integrity sha512-Q4ZuBAe2FUsKtyQJoQHlvP8OvBERxO3jEmy1I7hcRXcJBGGHFh/aJBswbXuS9sgrDH2QUO8ilkwNPHvHMd8clg==
Expand Down Expand Up @@ -4128,6 +4142,25 @@ object.assign@^4.1.4:
has-symbols "^1.0.3"
object-keys "^1.1.1"

object.fromentries@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/object.fromentries/-/object.fromentries-2.0.6.tgz#cdb04da08c539cffa912dcd368b886e0904bfa73"
integrity sha512-VciD13dswC4j1Xt5394WR4MzmAQmlgN72phd/riNp9vtD7tp4QQWJ0R4wvclXcafgcYK8veHRed2W6XeGBvcfg==
dependencies:
call-bind "^1.0.2"
define-properties "^1.1.4"
es-abstract "^1.20.4"

object.groupby@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/object.groupby/-/object.groupby-1.0.0.tgz#cb29259cf90f37e7bac6437686c1ea8c916d12a9"
integrity sha512-70MWG6NfRH9GnbZOikuhPPYzpUpof9iW2J9E4dW7FXTqPNb6rllE6u39SKwwiNh8lCwX3DDb5OgcKGiEBrTTyw==
dependencies:
call-bind "^1.0.2"
define-properties "^1.2.0"
es-abstract "^1.21.2"
get-intrinsic "^1.2.1"

object.values@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/object.values/-/object.values-1.1.6.tgz#4abbaa71eba47d63589d402856f908243eea9b1d"
Expand Down Expand Up @@ -4561,6 +4594,15 @@ resolve@^1.10.0, resolve@^1.20.0, resolve@^1.22.1:
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

resolve@^1.22.3:
version "1.22.3"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.3.tgz#4b4055349ffb962600972da1fdc33c46a4eb3283"
integrity sha512-P8ur/gp/AmbEzjr729bZnLjXK5Z+4P0zhIJgBgzqRih7hL7BOukHGtSTA3ACMY467GRFz3duQsi0bDZdR7DKdw==
dependencies:
is-core-module "^2.12.0"
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

responselike@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/responselike/-/responselike-2.0.1.tgz#9a0bc8fdc252f3fb1cca68b016591059ba1422bc"
Expand Down Expand Up @@ -5171,7 +5213,7 @@ ts-node@^10.0.0, ts-node@^10.8.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tsconfig-paths@^3.14.1:
tsconfig-paths@^3.14.2:
version "3.14.2"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.2.tgz#6e32f1f79412decd261f92d633a9dc1cfa99f088"
integrity sha512-o/9iXgCYc5L/JxCHPe3Hvh8Q/2xm5Z+p18PESBU6Ff33695QnCHBEjcytY2q19ua7Mbl/DavtBOLq+oG0RCL+g==
Expand Down

0 comments on commit 6b40989

Please sign in to comment.