├── .circleci └── config.yml ├── .eslintignore ├── .eslintrc.js ├── .github └── FUNDING.yml ├── .gitignore ├── .husky └── pre-commit ├── .mocharc.json ├── .npmrc ├── .nycrc.json ├── .prettierignore ├── .prettierrc.json ├── .releaserc.yml ├── LICENSE-APACHE-2.0 ├── LICENSE-MIT ├── README.md ├── __snapshots__ └── test │ ├── package.test.ts.js │ └── random │ └── alea.test.ts.js ├── babel.config.js ├── dev-lib └── dist │ ├── esm │ ├── index.d.ts │ └── index.js │ ├── index.d.ts │ ├── index.js │ └── umd │ ├── index.d.ts │ └── index.js ├── docs ├── .nojekyll ├── assets │ ├── highlight.css │ ├── icons.css │ ├── icons.png │ ├── icons@2x.png │ ├── main.js │ ├── search.js │ ├── style.css │ ├── widgets.png │ └── widgets@2x.png ├── index.html ├── interfaces │ ├── ColorObject.html │ ├── FullColorObject.html │ ├── HSV.html │ ├── RGB.html │ ├── RGBA.html │ └── RNG.html ├── modules.html └── modules │ └── default.html ├── interop.js ├── package-lock.json ├── package.json ├── renovate.json ├── rollup.build.js ├── src ├── deep-object-assign.ts ├── entry-esnext.ts ├── entry-peer.ts ├── entry-standalone.ts ├── index.ts ├── random │ ├── alea.ts │ └── index.ts ├── shared │ ├── activator.css │ ├── activator.d.ts │ ├── activator.js │ ├── bootstrap.css │ ├── color-picker.css │ ├── color-picker.d.ts │ ├── color-picker.js │ ├── configurator-types.ts │ ├── configurator.css │ ├── configurator.d.ts │ ├── configurator.js │ ├── hammer.d.ts │ ├── hammer.js │ ├── index.ts │ ├── popup.css │ ├── popup.d.ts │ ├── popup.js │ ├── validator.d.ts │ └── validator.js └── util.ts ├── test ├── add-class-name.test.ts ├── add-css-text.test.ts ├── binary-search-custom.test.ts ├── bridge-object.test.ts ├── copy-and-extend-array.test.ts ├── copy-array.test.ts ├── deep-extend.test.ts ├── deep-object-assign.errors.ts ├── deep-object-assign.test.ts ├── for-each.test.ts ├── get-absolute.test.ts ├── helpers │ └── index.ts ├── hex-to-rgb.test.ts ├── hsv-to-rgb.test.ts ├── insert-sort.test.ts ├── is-valid-hex.test.ts ├── is-valid-rgb.test.ts ├── is-valid-rgba.test.ts ├── package.test.ts ├── parse-color.test.ts ├── random │ └── alea.test.ts ├── remove-class-name.test.ts ├── remove-css-text.test.ts ├── rgb-to-hex.test.ts ├── selective-deep-extend.test.ts ├── selective-extend.test.ts ├── throttle.test.ts ├── top-most.test.ts └── util.test.js ├── tsconfig.check.json ├── tsconfig.code.json ├── tsconfig.docs.json ├── tsconfig.json ├── tsconfig.lint.json ├── tsconfig.types.json └── typedoc.json /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | executors: 4 | node: 5 | docker: 6 | - image: cimg/node:21.2.0 7 | resource_class: small 8 | working_directory: ~/repo 9 | environment: 10 | GIT_AUTHOR_EMAIL: visjsbot@gmail.com 11 | GIT_AUTHOR_NAME: vis-bot 12 | GIT_COMMITTER_EMAIL: visjsbot@gmail.com 13 | GIT_COMMITTER_NAME: vis-bot 14 | 15 | node-interop: 16 | docker: 17 | - image: cimg/node:21.2.0-browsers 18 | resource_class: "medium+" 19 | working_directory: ~/repo 20 | environment: 21 | GIT_AUTHOR_EMAIL: visjsbot@gmail.com 22 | GIT_AUTHOR_NAME: vis-bot 23 | GIT_COMMITTER_EMAIL: visjsbot@gmail.com 24 | GIT_COMMITTER_NAME: vis-bot 25 | 26 | jobs: 27 | prepare: 28 | executor: node 29 | 30 | steps: 31 | - checkout 32 | 33 | - run: npm ci 34 | 35 | - persist_to_workspace: 36 | root: . 37 | paths: 38 | - "*" 39 | 40 | build: 41 | executor: node 42 | 43 | steps: 44 | - attach_workspace: 45 | at: . 46 | 47 | - run: npm run build 48 | 49 | - persist_to_workspace: 50 | root: . 51 | paths: 52 | - "declarations" 53 | - "esnext" 54 | - "peer" 55 | - "standalone" 56 | 57 | lint: 58 | executor: node 59 | 60 | steps: 61 | - attach_workspace: 62 | at: . 63 | 64 | - run: npm run style 65 | - run: npm run lint 66 | 67 | type_check: 68 | executor: node 69 | 70 | steps: 71 | - attach_workspace: 72 | at: . 73 | 74 | - run: npm run test:types:tsc 75 | 76 | - run: npm run test:types:check-dts 77 | 78 | test_unit: 79 | executor: node 80 | 81 | steps: 82 | - attach_workspace: 83 | at: . 84 | 85 | - run: npm run test:coverage 86 | 87 | test_interop: 88 | executor: node-interop 89 | 90 | steps: 91 | - attach_workspace: 92 | at: . 93 | 94 | - run: npm pack 95 | 96 | - run: 97 | command: npm run test:interop -- --tmp-dir ../interop 98 | # This runs multiple things in parallel and only reports start and end 99 | # of individual commands, the full logs are printed at the end. Due to 100 | # this it's quite likely to go a long time without any output and 101 | # still finish successfully. 102 | no_output_timeout: 60m 103 | 104 | - store_artifacts: 105 | path: ../interop/repos/logs 106 | destination: interop-logs 107 | 108 | release: 109 | executor: node 110 | 111 | steps: 112 | - attach_workspace: 113 | at: . 114 | 115 | - run: 116 | name: Prepare NPM 117 | command: | 118 | npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN 119 | 120 | - run: 121 | name: Release 122 | command: | 123 | npx semantic-release 124 | 125 | workflows: 126 | version: 2 127 | 128 | build: 129 | jobs: 130 | - prepare 131 | 132 | - build: 133 | requires: 134 | - prepare 135 | 136 | - lint: 137 | requires: 138 | - prepare 139 | 140 | - type_check: 141 | requires: 142 | - prepare 143 | 144 | - test_unit: 145 | requires: 146 | - prepare 147 | - build 148 | 149 | - test_interop: 150 | requires: 151 | - prepare 152 | - build 153 | 154 | - release: 155 | requires: 156 | - prepare 157 | - build 158 | - lint 159 | - type_check 160 | - test_unit 161 | - test_interop 162 | filters: 163 | branches: 164 | only: 165 | - master 166 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | !/*?.?* 4 | !/.?*.?* 5 | 6 | !/.circleci 7 | !/dev-lib 8 | !/src 9 | !/test 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [require.resolve("vis-dev-utils/eslint-shareable-config")], 3 | overrides: [ 4 | { 5 | files: ["./test/util.test.js"], 6 | rules: { 7 | "no-var": "off", 8 | }, 9 | }, 10 | ], 11 | }; 12 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: visjs 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with a single custom sponsorship URL 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .nyc_output 2 | /.stryker-tmp/ 3 | /declarations/ 4 | /esnext/ 5 | /index.d.ts 6 | /index.js 7 | /peer/ 8 | /reports/ 9 | /standalone/ 10 | coverage 11 | node_modules 12 | vis-util-*.tgz 13 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npx lint-staged 5 | -------------------------------------------------------------------------------- /.mocharc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extension": ["ts", "js"], 3 | "require": ["vis-dev-utils/babel-register"], 4 | "spec": ["./test/**/*.test.js", "./test/**/*.test.ts"] 5 | } 6 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | sign-git-commit=true 2 | -------------------------------------------------------------------------------- /.nycrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "exclude": ["**/*.test.*", "**/*.d.ts"], 3 | "extension": ["ts", "js"], 4 | "include": ["src/**/*"], 5 | "instrument": false, 6 | "reporter": ["text-summary", "lcov"], 7 | "sourceMap": false 8 | } 9 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .eslintignore -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /.releaserc.yml: -------------------------------------------------------------------------------- 1 | branch: master 2 | plugins: 3 | - "@semantic-release/commit-analyzer" 4 | - "@semantic-release/release-notes-generator" 5 | - "@semantic-release/npm" 6 | - "@semantic-release/github" 7 | -------------------------------------------------------------------------------- /LICENSE-APACHE-2.0: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017 Almende B.V. 4 | Copyright (c) 2018-2019 Contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vis-util 2 | 3 | [![Greenkeeper badge](https://badges.greenkeeper.io/visjs/vis-util.svg)](https://greenkeeper.io/) 4 | 5 | This module is part of the [visjs project](https://github.com/visjs) 6 | -------------------------------------------------------------------------------- /__snapshots__/test/package.test.ts.js: -------------------------------------------------------------------------------- 1 | exports['Package Exported files 1'] = { 2 | "name": "vis-util ", 3 | "files": [ 4 | " LICENSE-APACHE-2.0", 5 | " LICENSE-MIT", 6 | " README.md", 7 | " declarations/deep-object-assign.d.ts", 8 | " declarations/deep-object-assign.d.ts.map", 9 | " declarations/entry-esnext.d.ts", 10 | " declarations/entry-esnext.d.ts.map", 11 | " declarations/entry-peer.d.ts", 12 | " declarations/entry-peer.d.ts.map", 13 | " declarations/entry-standalone.d.ts", 14 | " declarations/entry-standalone.d.ts.map", 15 | " declarations/index.d.ts", 16 | " declarations/index.d.ts.map", 17 | " declarations/random/alea.d.ts", 18 | " declarations/random/alea.d.ts.map", 19 | " declarations/random/index.d.ts", 20 | " declarations/random/index.d.ts.map", 21 | " declarations/shared/configurator-types.d.ts", 22 | " declarations/shared/configurator-types.d.ts.map", 23 | " declarations/shared/index.d.ts", 24 | " declarations/shared/index.d.ts.map", 25 | " declarations/util.d.ts", 26 | " declarations/util.d.ts.map", 27 | " esnext/esm/index.d.ts", 28 | " esnext/esm/index.js", 29 | " esnext/esm/vis-util.d.ts", 30 | " esnext/esm/vis-util.js", 31 | " esnext/esm/vis-util.js.map", 32 | " esnext/esm/vis-util.min.d.ts", 33 | " esnext/esm/vis-util.min.js", 34 | " esnext/esm/vis-util.min.js.map", 35 | " esnext/index.d.ts", 36 | " esnext/index.js", 37 | " esnext/styles/activator.css", 38 | " esnext/styles/bootstrap.css", 39 | " esnext/styles/color-picker.css", 40 | " esnext/styles/configurator.css", 41 | " esnext/styles/popup.css", 42 | " esnext/umd/index.d.ts", 43 | " esnext/umd/index.js", 44 | " esnext/umd/vis-util.d.ts", 45 | " esnext/umd/vis-util.js", 46 | " esnext/umd/vis-util.js.map", 47 | " esnext/umd/vis-util.min.d.ts", 48 | " esnext/umd/vis-util.min.js", 49 | " esnext/umd/vis-util.min.js.map", 50 | " package.json", 51 | " peer/esm/index.d.ts", 52 | " peer/esm/index.js", 53 | " peer/esm/vis-util.d.ts", 54 | " peer/esm/vis-util.js", 55 | " peer/esm/vis-util.js.map", 56 | " peer/esm/vis-util.min.d.ts", 57 | " peer/esm/vis-util.min.js", 58 | " peer/esm/vis-util.min.js.map", 59 | " peer/index.d.ts", 60 | " peer/index.js", 61 | " peer/styles/activator.css", 62 | " peer/styles/bootstrap.css", 63 | " peer/styles/color-picker.css", 64 | " peer/styles/configurator.css", 65 | " peer/styles/popup.css", 66 | " peer/umd/index.d.ts", 67 | " peer/umd/index.js", 68 | " peer/umd/vis-util.d.ts", 69 | " peer/umd/vis-util.js", 70 | " peer/umd/vis-util.js.map", 71 | " peer/umd/vis-util.min.d.ts", 72 | " peer/umd/vis-util.min.js", 73 | " peer/umd/vis-util.min.js.map", 74 | " standalone/esm/index.d.ts", 75 | " standalone/esm/index.js", 76 | " standalone/esm/vis-util.d.ts", 77 | " standalone/esm/vis-util.js", 78 | " standalone/esm/vis-util.js.map", 79 | " standalone/esm/vis-util.min.d.ts", 80 | " standalone/esm/vis-util.min.js", 81 | " standalone/esm/vis-util.min.js.map", 82 | " standalone/index.d.ts", 83 | " standalone/index.js", 84 | " standalone/styles/activator.css", 85 | " standalone/styles/bootstrap.css", 86 | " standalone/styles/color-picker.css", 87 | " standalone/styles/configurator.css", 88 | " standalone/styles/popup.css", 89 | " standalone/umd/index.d.ts", 90 | " standalone/umd/index.js", 91 | " standalone/umd/vis-util.d.ts", 92 | " standalone/umd/vis-util.js", 93 | " standalone/umd/vis-util.js.map", 94 | " standalone/umd/vis-util.min.d.ts", 95 | " standalone/umd/vis-util.min.js", 96 | " standalone/umd/vis-util.min.js.map" 97 | ] 98 | } 99 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | exclude: require("vis-dev-utils").BABEL_IGNORE_RE, 3 | presets: [["vis-dev-utils/babel-preset", { ts: true }]], 4 | }; 5 | -------------------------------------------------------------------------------- /dev-lib/dist/esm/index.d.ts: -------------------------------------------------------------------------------- 1 | export * from "./vis-util"; 2 | -------------------------------------------------------------------------------- /dev-lib/dist/esm/index.js: -------------------------------------------------------------------------------- 1 | export * from "./vis-util"; 2 | -------------------------------------------------------------------------------- /dev-lib/dist/index.d.ts: -------------------------------------------------------------------------------- 1 | export * from "./esm"; 2 | -------------------------------------------------------------------------------- /dev-lib/dist/index.js: -------------------------------------------------------------------------------- 1 | export * from "./esm"; 2 | -------------------------------------------------------------------------------- /dev-lib/dist/umd/index.d.ts: -------------------------------------------------------------------------------- 1 | export * from "./vis-util"; 2 | -------------------------------------------------------------------------------- /dev-lib/dist/umd/index.js: -------------------------------------------------------------------------------- 1 | export * from "./vis-util"; 2 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false. -------------------------------------------------------------------------------- /docs/assets/highlight.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --light-code-background: #F5F5F5; 3 | --dark-code-background: #1E1E1E; 4 | } 5 | 6 | @media (prefers-color-scheme: light) { :root { 7 | --code-background: var(--light-code-background); 8 | } } 9 | 10 | @media (prefers-color-scheme: dark) { :root { 11 | --code-background: var(--dark-code-background); 12 | } } 13 | 14 | body.light { 15 | --code-background: var(--light-code-background); 16 | } 17 | 18 | body.dark { 19 | --code-background: var(--dark-code-background); 20 | } 21 | 22 | pre, code { background: var(--code-background); } 23 | -------------------------------------------------------------------------------- /docs/assets/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/visjs/vis-util/cdd028dcc7eebcc4b8364920569d2933c9080912/docs/assets/icons.png -------------------------------------------------------------------------------- /docs/assets/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/visjs/vis-util/cdd028dcc7eebcc4b8364920569d2933c9080912/docs/assets/icons@2x.png -------------------------------------------------------------------------------- /docs/assets/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/visjs/vis-util/cdd028dcc7eebcc4b8364920569d2933c9080912/docs/assets/widgets.png -------------------------------------------------------------------------------- /docs/assets/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/visjs/vis-util/cdd028dcc7eebcc4b8364920569d2933c9080912/docs/assets/widgets@2x.png -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

vis-util

2 | 3 |

vis-util

4 |
5 |

Greenkeeper badge

6 |

This module is part of the visjs project

7 |

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/ColorObject.html: -------------------------------------------------------------------------------- 1 | ColorObject | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ColorObject

Hierarchy

  • ColorObject

Index

Properties

background?: string
border?: string
highlight?: string | { background?: string; border?: string }
hover?: string | { background?: string; border?: string }

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/FullColorObject.html: -------------------------------------------------------------------------------- 1 | FullColorObject | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface FullColorObject

Hierarchy

  • FullColorObject

Index

Properties

background: string
border: string
highlight: { background: string; border: string }

Type declaration

  • background: string
  • border: string
hover: { background: string; border: string }

Type declaration

  • background: string
  • border: string

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/HSV.html: -------------------------------------------------------------------------------- 1 | HSV | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface HSV

2 |

Hue, Saturation, Value.

3 |

Hierarchy

  • HSV

Index

Properties

Properties

h: number
4 |

Hue <0, 1>.

5 |
s: number
6 |

Saturation <0, 1>.

7 |
v: number
8 |

Value <0, 1>.

9 |

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/RGB.html: -------------------------------------------------------------------------------- 1 | RGB | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface RGB

2 |

Red, Green, Blue.

3 |

Hierarchy

  • RGB

Index

Properties

Properties

b: number
4 |

Blue <0, 255> integer.

5 |
g: number
6 |

Green <0, 255> integer.

7 |
r: number
8 |

Red <0, 255> integer.

9 |

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/RGBA.html: -------------------------------------------------------------------------------- 1 | RGBA | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface RGBA

2 |

Red, Green, Blue, Alpha.

3 |

Hierarchy

  • RGBA

Index

Properties

Properties

a: number
4 |

Alpha <0, 1>.

5 |
b: number
6 |

Blue <0, 255> integer.

7 |
g: number
8 |

Green <0, 255> integer.

9 |
r: number
10 |

Red <0, 255> integer.

11 |

Generated using TypeDoc

-------------------------------------------------------------------------------- /docs/interfaces/RNG.html: -------------------------------------------------------------------------------- 1 | RNG | vis-util
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface RNG

Hierarchy

  • RNG

Callable

  • RNG(): number

Index

Properties

algorithm: string
4 |

The algorithm gehind this instance.

5 |
seed: Mashable[]
6 |

The seed used to seed this instance.

7 |
version: string
8 |

The version of this instance.

9 |

Methods

  • fract53(): number
  • uint32(): number

Generated using TypeDoc

-------------------------------------------------------------------------------- /interop.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // Ideally this would be just a Shell script but Windows exists and I don't want 4 | // to force Windows users to install Shell and configure it to be used to run 5 | // npm scripts (and break their Windows only projects?). 6 | 7 | const { spawnSync } = require("child_process"); 8 | 9 | const { status } = spawnSync( 10 | "npx", 11 | [ 12 | "test-e2e-interop", 13 | // This is the shared stuff used in all of our projects. 14 | "--config", 15 | "./node_modules/vis-dev-utils/dist/interop/base-config.json", 16 | // This is specific to this project. 17 | "--project", 18 | "vis-charts https://github.com/visjs/vis-charts.git", 19 | "--project", 20 | "vis-data https://github.com/visjs/vis-data.git", 21 | "--project", 22 | "vis-graph3d https://github.com/visjs/vis-graph3d.git", 23 | "--project", 24 | "vis-network https://github.com/visjs/vis-network.git", 25 | "--project", 26 | "vis-timeline https://github.com/visjs/vis-timeline.git", 27 | "--project", 28 | "vis-util ./vis-util-0.0.0-no-version.tgz", 29 | // Any additional options passed from the command line (e.g. a fail command 30 | // for debugging). 31 | ...process.argv.slice(2), 32 | ], 33 | { stdio: "inherit" } 34 | ); 35 | process.exitCode = status; 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vis-util", 3 | "version": "0.0.0-no-version", 4 | "description": "utilitie collection for visjs", 5 | "browser": "peer/umd/vis-util.min.js", 6 | "jsnext": "esnext/esm/vis-util.js", 7 | "main": "peer/umd/vis-util.js", 8 | "module": "peer/esm/vis-util.js", 9 | "types": "declarations/index.d.ts", 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/visjs/vis-util.git" 13 | }, 14 | "author": "Alex de Mulder ", 15 | "contributors": [ 16 | "Alex de Mulder ", 17 | "jos ", 18 | "Tomáš Vyčítal ", 19 | "Alexander Wunschik ", 20 | "wimrijnders ", 21 | "yotamberk ", 22 | "David Anderson ", 23 | "Ludo Stellingwerff ", 24 | "Ben Morton ", 25 | "Greg Kubisa ", 26 | "Kelvin Del Monte ", 27 | "Zuko Mgwili " 28 | ], 29 | "scripts": { 30 | "build": "run-s build:types build:code build:docs", 31 | "build:code": "rollup --bundleConfigAsCjs --config rollup.build.js", 32 | "build:docs": "typedoc", 33 | "build:types": "tsc -p tsconfig.types.json", 34 | "clean": "rimraf --glob \"{coverage,declarations,docs,esnext,peer,reports,standalone}\" \"index.{d.ts,js}\"", 35 | "style": "prettier --check .", 36 | "style-fix": "prettier --write .", 37 | "lint": "eslint --ext .js,.ts .", 38 | "lint-fix": "eslint --fix --ext .js,.ts .", 39 | "prepublishOnly": "npm run build", 40 | "test": "run-s test:unit test:types:check-dts test:types:tsc test:interop", 41 | "test:coverage": "cross-env BABEL_ENV=test-cov nyc mocha", 42 | "test:interop": "node interop.js", 43 | "test:interop:debug": "npm run test:interop -- --fail-command \"$SHELL\"", 44 | "test:types:check-dts": "cd test && check-dts", 45 | "test:types:tsc": "tsc --noemit --project tsconfig.check.json", 46 | "test:unit": "cross-env BABEL_ENV=test mocha", 47 | "type-check": "run-s test:types:*", 48 | "version": "npm run contributors:update && git add package.json", 49 | "contributors:update": "git-authors-cli", 50 | "preparepublish": "npm run contributors:update", 51 | "prepare": "husky install" 52 | }, 53 | "lint-staged": { 54 | "*.*": "prettier --write", 55 | "*.{js,ts}": "eslint --fix", 56 | ".*.*": "prettier --write", 57 | ".*.{js,ts}": "eslint --fix" 58 | }, 59 | "config": { 60 | "snap-shot-it": { 61 | "sortSnapshots": true, 62 | "useRelativePath": true 63 | } 64 | }, 65 | "engines": { 66 | "node": ">=8" 67 | }, 68 | "keywords": [ 69 | "util", 70 | "vis", 71 | "vis.js" 72 | ], 73 | "license": "(Apache-2.0 OR MIT)", 74 | "bugs": { 75 | "url": "https://github.com/visjs/vis-util/issues" 76 | }, 77 | "homepage": "https://github.com/visjs/vis-util", 78 | "files": [ 79 | "LICENSE*", 80 | "standalone", 81 | "peer", 82 | "esnext", 83 | "declarations" 84 | ], 85 | "funding": { 86 | "type": "opencollective", 87 | "url": "https://opencollective.com/visjs" 88 | }, 89 | "peerDependencies": { 90 | "@egjs/hammerjs": "^2.0.0", 91 | "component-emitter": "^1.3.0 || ^2.0.0" 92 | }, 93 | "devDependencies": { 94 | "@egjs/hammerjs": "2.0.17", 95 | "@types/chai": "4.3.11", 96 | "@types/jsdom-global": "^3.0.4", 97 | "@types/mocha": "10.0.6", 98 | "@types/node": "20.10.3", 99 | "@types/sinon": "17.0.2", 100 | "assert": "2.1.0", 101 | "check-dts": "0.7.2", 102 | "component-emitter": "2.0.0", 103 | "cross-env": "^7.0.3", 104 | "eslint": "8.55.0", 105 | "git-authors-cli": "1.0.47", 106 | "husky": "8.0.3", 107 | "jsdom": "22.1.0", 108 | "jsdom-global": "3.0.2", 109 | "lint-staged": "15.2.0", 110 | "mocha": "10.2.0", 111 | "npm-run-all": "4.1.5", 112 | "nyc": "15.1.0", 113 | "rimraf": "5.0.5", 114 | "sazerac": "2.0.0", 115 | "semantic-release": "22.0.8", 116 | "sinon": "17.0.1", 117 | "snap-shot-it": "7.9.10", 118 | "typedoc": "0.25.4", 119 | "vis-dev-utils": "4.0.45" 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["config:base"], 3 | "packageRules": [ 4 | { 5 | "updateTypes": ["minor", "patch", "pin", "digest"], 6 | "automerge": true 7 | } 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /rollup.build.js: -------------------------------------------------------------------------------- 1 | import packageJSON from "./package.json"; 2 | import { generateRollupConfiguration } from "vis-dev-utils"; 3 | 4 | export default generateRollupConfiguration({ 5 | header: { name: "vis-util" }, 6 | libraryFilename: "vis-util", 7 | entryPoint: "./src", 8 | packageJSON, 9 | tsconfig: "tsconfig.code.json", 10 | globals: { 11 | "@egjs/hammerjs": "Hammer", 12 | "component-emitter": "Emitter", 13 | }, 14 | copyTargets: { 15 | bundle: [{ src: "src/**/*.css", dest: "styles" }], 16 | }, 17 | }); 18 | -------------------------------------------------------------------------------- /src/deep-object-assign.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Use this symbol to delete properies in deepObjectAssign. 3 | */ 4 | export const DELETE = Symbol("DELETE"); 5 | 6 | /** 7 | * Turns `undefined` into `undefined | typeof DELETE` and makes everything 8 | * partial. Intended to be used with `deepObjectAssign`. 9 | */ 10 | export type Assignable = T extends undefined 11 | ? 12 | | (T extends Function 13 | ? T 14 | : T extends object 15 | ? { [Key in keyof T]?: Assignable | undefined } 16 | : T) 17 | | typeof DELETE 18 | : T extends Function 19 | ? T | undefined 20 | : T extends object 21 | ? { [Key in keyof T]?: Assignable | undefined } 22 | : T | undefined; 23 | 24 | /** 25 | * Pure version of deepObjectAssign, it doesn't modify any of it's arguments. 26 | * 27 | * @param base - The base object that fullfils the whole interface T. 28 | * @param updates - Updates that may change or delete props. 29 | * @returns A brand new instance with all the supplied objects deeply merged. 30 | */ 31 | export function pureDeepObjectAssign( 32 | base: T, 33 | ...updates: Assignable[] 34 | ): T { 35 | return deepObjectAssign({} as any, base, ...updates); 36 | } 37 | 38 | /** 39 | * Deep version of object assign with additional deleting by the DELETE symbol. 40 | * 41 | * @param target - The object that will be augmented using the sources. 42 | * @param sources - Objects to be deeply merged into the target. 43 | * @returns The target (same instance). 44 | */ 45 | export function deepObjectAssign(target: T, ...sources: Assignable[]): T; 46 | /** 47 | * Deep version of object assign with additional deleting by the DELETE symbol. 48 | * 49 | * @param values - Objects to be deeply merged. 50 | * @returns The first object from values. 51 | */ 52 | export function deepObjectAssign(...values: readonly any[]): any { 53 | const merged = deepObjectAssignNonentry(...values); 54 | stripDelete(merged); 55 | return merged; 56 | } 57 | 58 | /** 59 | * Deep version of object assign with additional deleting by the DELETE symbol. 60 | * 61 | * @remarks 62 | * This doesn't strip the DELETE symbols so they may end up in the final object. 63 | * @param values - Objects to be deeply merged. 64 | * @returns The first object from values. 65 | */ 66 | function deepObjectAssignNonentry(...values: readonly any[]): any { 67 | if (values.length < 2) { 68 | return values[0]; 69 | } else if (values.length > 2) { 70 | return deepObjectAssignNonentry( 71 | deepObjectAssign(values[0], values[1]), 72 | ...values.slice(2) 73 | ); 74 | } 75 | 76 | const a = values[0]; 77 | const b = values[1]; 78 | 79 | if (a instanceof Date && b instanceof Date) { 80 | a.setTime(b.getTime()); 81 | return a; 82 | } 83 | 84 | for (const prop of Reflect.ownKeys(b)) { 85 | if (!Object.prototype.propertyIsEnumerable.call(b, prop)) { 86 | // Ignore nonenumerable props, Object.assign() would do the same. 87 | } else if (b[prop] === DELETE) { 88 | delete a[prop]; 89 | } else if ( 90 | a[prop] !== null && 91 | b[prop] !== null && 92 | typeof a[prop] === "object" && 93 | typeof b[prop] === "object" && 94 | !Array.isArray(a[prop]) && 95 | !Array.isArray(b[prop]) 96 | ) { 97 | a[prop] = deepObjectAssignNonentry(a[prop], b[prop]); 98 | } else { 99 | a[prop] = clone(b[prop]); 100 | } 101 | } 102 | 103 | return a; 104 | } 105 | 106 | /** 107 | * Deep clone given object or array. In case of primitive simply return. 108 | * 109 | * @param a - Anything. 110 | * @returns Deep cloned object/array or unchanged a. 111 | */ 112 | function clone(a: any): any { 113 | if (Array.isArray(a)) { 114 | return a.map((value: any): any => clone(value)); 115 | } else if (typeof a === "object" && a !== null) { 116 | if (a instanceof Date) { 117 | return new Date(a.getTime()); 118 | } 119 | return deepObjectAssignNonentry({}, a); 120 | } else { 121 | return a; 122 | } 123 | } 124 | 125 | /** 126 | * Strip DELETE from given object. 127 | * 128 | * @param a - Object which may contain DELETE but won't after this is executed. 129 | */ 130 | function stripDelete(a: any): void { 131 | for (const prop of Object.keys(a)) { 132 | if (a[prop] === DELETE) { 133 | delete a[prop]; 134 | } else if (typeof a[prop] === "object" && a[prop] !== null) { 135 | stripDelete(a[prop]); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/entry-esnext.ts: -------------------------------------------------------------------------------- 1 | export * from "./deep-object-assign"; 2 | export * from "./random"; 3 | export * from "./shared"; 4 | export * from "./util"; 5 | -------------------------------------------------------------------------------- /src/entry-peer.ts: -------------------------------------------------------------------------------- 1 | export * from "./entry-esnext"; 2 | -------------------------------------------------------------------------------- /src/entry-standalone.ts: -------------------------------------------------------------------------------- 1 | export * from "./entry-esnext"; 2 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | // New API (tree shakeable). 2 | export * from "./entry-esnext"; 3 | 4 | // Old API (treeshakeable only if completely unused). 5 | import * as util from "./util"; 6 | export default util; 7 | -------------------------------------------------------------------------------- /src/random/alea.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Seedable, fast and reasonably good (not crypto but more than okay for our 3 | * needs) random number generator. 4 | * 5 | * @remarks 6 | * Adapted from {@link https://web.archive.org/web/20110429100736/http://baagoe.com:80/en/RandomMusings/javascript}. 7 | * Original algorithm created by Johannes Baagøe \ in 2010. 8 | */ 9 | 10 | /** 11 | * Random number generator. 12 | */ 13 | export interface RNG { 14 | /** Returns \<0, 1). Faster than [[fract53]]. */ 15 | (): number; 16 | /** Returns \<0, 1). Provides more precise data. */ 17 | fract53(): number; 18 | /** Returns \<0, 2^32). */ 19 | uint32(): number; 20 | 21 | /** The algorithm gehind this instance. */ 22 | algorithm: string; 23 | /** The seed used to seed this instance. */ 24 | seed: Mashable[]; 25 | /** The version of this instance. */ 26 | version: string; 27 | } 28 | 29 | /** 30 | * Create a seeded pseudo random generator based on Alea by Johannes Baagøe. 31 | * 32 | * @param seed - All supplied arguments will be used as a seed. In case nothing 33 | * is supplied the current time will be used to seed the generator. 34 | * @returns A ready to use seeded generator. 35 | */ 36 | export function Alea(...seed: Mashable[]): RNG { 37 | return AleaImplementation(seed.length ? seed : [Date.now()]); 38 | } 39 | 40 | /** 41 | * An implementation of [[Alea]] without user input validation. 42 | * 43 | * @param seed - The data that will be used to seed the generator. 44 | * @returns A ready to use seeded generator. 45 | */ 46 | function AleaImplementation(seed: Mashable[]): RNG { 47 | let [s0, s1, s2] = mashSeed(seed); 48 | let c = 1; 49 | 50 | const random: RNG = (): number => { 51 | const t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32 52 | s0 = s1; 53 | s1 = s2; 54 | return (s2 = t - (c = t | 0)); 55 | }; 56 | 57 | random.uint32 = (): number => random() * 0x100000000; // 2^32 58 | 59 | random.fract53 = (): number => 60 | random() + ((random() * 0x200000) | 0) * 1.1102230246251565e-16; // 2^-53 61 | 62 | random.algorithm = "Alea"; 63 | random.seed = seed; 64 | random.version = "0.9"; 65 | 66 | return random; 67 | } 68 | 69 | /** 70 | * Turn arbitrary data into values [[AleaImplementation]] can use to generate 71 | * random numbers. 72 | * 73 | * @param seed - Arbitrary data that will be used as the seed. 74 | * @returns Three numbers to use as initial values for [[AleaImplementation]]. 75 | */ 76 | function mashSeed(...seed: Mashable[]): [number, number, number] { 77 | const mash = Mash(); 78 | 79 | let s0 = mash(" "); 80 | let s1 = mash(" "); 81 | let s2 = mash(" "); 82 | 83 | for (let i = 0; i < seed.length; i++) { 84 | s0 -= mash(seed[i]); 85 | if (s0 < 0) { 86 | s0 += 1; 87 | } 88 | s1 -= mash(seed[i]); 89 | if (s1 < 0) { 90 | s1 += 1; 91 | } 92 | s2 -= mash(seed[i]); 93 | if (s2 < 0) { 94 | s2 += 1; 95 | } 96 | } 97 | 98 | return [s0, s1, s2]; 99 | } 100 | 101 | /** 102 | * Values of these types can be used as a seed. 103 | */ 104 | export type Mashable = number | string | boolean | object | bigint; 105 | 106 | /** 107 | * Create a new mash function. 108 | * 109 | * @returns A nonpure function that takes arbitrary [[Mashable]] data and turns 110 | * them into numbers. 111 | */ 112 | function Mash(): (data: Mashable) => number { 113 | let n = 0xefc8249d; 114 | 115 | return function (data): number { 116 | const string = data.toString(); 117 | for (let i = 0; i < string.length; i++) { 118 | n += string.charCodeAt(i); 119 | let h = 0.02519603282416938 * n; 120 | n = h >>> 0; 121 | h -= n; 122 | h *= n; 123 | n = h >>> 0; 124 | h -= n; 125 | n += h * 0x100000000; // 2^32 126 | } 127 | return (n >>> 0) * 2.3283064365386963e-10; // 2^-32 128 | }; 129 | } 130 | -------------------------------------------------------------------------------- /src/random/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./alea"; 2 | -------------------------------------------------------------------------------- /src/shared/activator.css: -------------------------------------------------------------------------------- 1 | .vis-overlay { 2 | position: absolute; 3 | top: 0px; 4 | right: 0px; 5 | bottom: 0px; 6 | left: 0px; 7 | 8 | /* Must be displayed above for example selected Timeline items */ 9 | z-index: 10; 10 | } 11 | 12 | .vis-active { 13 | box-shadow: 0 0 10px #86d5f8; 14 | } 15 | -------------------------------------------------------------------------------- /src/shared/activator.d.ts: -------------------------------------------------------------------------------- 1 | export const Activator: any; 2 | -------------------------------------------------------------------------------- /src/shared/activator.js: -------------------------------------------------------------------------------- 1 | import Emitter from "component-emitter"; 2 | import { Hammer } from "./hammer"; 3 | 4 | /** 5 | * Turn an element into an clickToUse element. 6 | * When not active, the element has a transparent overlay. When the overlay is 7 | * clicked, the mode is changed to active. 8 | * When active, the element is displayed with a blue border around it, and 9 | * the interactive contents of the element can be used. When clicked outside 10 | * the element, the elements mode is changed to inactive. 11 | * 12 | * @param {Element} container 13 | * @class Activator 14 | */ 15 | export function Activator(container) { 16 | this._cleanupQueue = []; 17 | 18 | this.active = false; 19 | 20 | this._dom = { 21 | container, 22 | overlay: document.createElement("div"), 23 | }; 24 | 25 | this._dom.overlay.classList.add("vis-overlay"); 26 | 27 | this._dom.container.appendChild(this._dom.overlay); 28 | this._cleanupQueue.push(() => { 29 | this._dom.overlay.parentNode.removeChild(this._dom.overlay); 30 | }); 31 | 32 | const hammer = Hammer(this._dom.overlay); 33 | hammer.on("tap", this._onTapOverlay.bind(this)); 34 | this._cleanupQueue.push(() => { 35 | hammer.destroy(); 36 | // FIXME: cleaning up hammer instances doesn't work (Timeline not removed 37 | // from memory) 38 | }); 39 | 40 | // block all touch events (except tap) 41 | const events = [ 42 | "tap", 43 | "doubletap", 44 | "press", 45 | "pinch", 46 | "pan", 47 | "panstart", 48 | "panmove", 49 | "panend", 50 | ]; 51 | events.forEach((event) => { 52 | hammer.on(event, (event) => { 53 | event.srcEvent.stopPropagation(); 54 | }); 55 | }); 56 | 57 | // attach a click event to the window, in order to deactivate when clicking outside the timeline 58 | if (document && document.body) { 59 | this._onClick = (event) => { 60 | if (!_hasParent(event.target, container)) { 61 | this.deactivate(); 62 | } 63 | }; 64 | document.body.addEventListener("click", this._onClick); 65 | this._cleanupQueue.push(() => { 66 | document.body.removeEventListener("click", this._onClick); 67 | }); 68 | } 69 | 70 | // prepare escape key listener for deactivating when active 71 | this._escListener = (event) => { 72 | if ( 73 | "key" in event 74 | ? event.key === "Escape" 75 | : event.keyCode === 27 /* the keyCode is for IE11 */ 76 | ) { 77 | this.deactivate(); 78 | } 79 | }; 80 | } 81 | 82 | // turn into an event emitter 83 | Emitter(Activator.prototype); 84 | 85 | // The currently active activator 86 | Activator.current = null; 87 | 88 | /** 89 | * Destroy the activator. Cleans up all created DOM and event listeners 90 | */ 91 | Activator.prototype.destroy = function () { 92 | this.deactivate(); 93 | 94 | for (const callback of this._cleanupQueue.splice(0).reverse()) { 95 | callback(); 96 | } 97 | }; 98 | 99 | /** 100 | * Activate the element 101 | * Overlay is hidden, element is decorated with a blue shadow border 102 | */ 103 | Activator.prototype.activate = function () { 104 | // we allow only one active activator at a time 105 | if (Activator.current) { 106 | Activator.current.deactivate(); 107 | } 108 | Activator.current = this; 109 | 110 | this.active = true; 111 | this._dom.overlay.style.display = "none"; 112 | this._dom.container.classList.add("vis-active"); 113 | 114 | this.emit("change"); 115 | this.emit("activate"); 116 | 117 | // ugly hack: bind ESC after emitting the events, as the Network rebinds all 118 | // keyboard events on a 'change' event 119 | document.body.addEventListener("keydown", this._escListener); 120 | }; 121 | 122 | /** 123 | * Deactivate the element 124 | * Overlay is displayed on top of the element 125 | */ 126 | Activator.prototype.deactivate = function () { 127 | this.active = false; 128 | this._dom.overlay.style.display = "block"; 129 | this._dom.container.classList.remove("vis-active"); 130 | document.body.removeEventListener("keydown", this._escListener); 131 | 132 | this.emit("change"); 133 | this.emit("deactivate"); 134 | }; 135 | 136 | /** 137 | * Handle a tap event: activate the container 138 | * 139 | * @param {Event} event The event 140 | * @private 141 | */ 142 | Activator.prototype._onTapOverlay = function (event) { 143 | // activate the container 144 | this.activate(); 145 | event.srcEvent.stopPropagation(); 146 | }; 147 | 148 | /** 149 | * Test whether the element has the requested parent element somewhere in 150 | * its chain of parent nodes. 151 | * 152 | * @param {HTMLElement} element 153 | * @param {HTMLElement} parent 154 | * @returns {boolean} Returns true when the parent is found somewhere in the 155 | * chain of parent nodes. 156 | * @private 157 | */ 158 | function _hasParent(element, parent) { 159 | while (element) { 160 | if (element === parent) { 161 | return true; 162 | } 163 | element = element.parentNode; 164 | } 165 | return false; 166 | } 167 | -------------------------------------------------------------------------------- /src/shared/bootstrap.css: -------------------------------------------------------------------------------- 1 | /* override some bootstrap styles screwing up the timelines css */ 2 | 3 | .vis [class*="span"] { 4 | min-height: 0; 5 | width: auto; 6 | } 7 | -------------------------------------------------------------------------------- /src/shared/color-picker.css: -------------------------------------------------------------------------------- 1 | div.vis-color-picker { 2 | position: absolute; 3 | top: 0px; 4 | left: 30px; 5 | margin-top: -140px; 6 | margin-left: 30px; 7 | width: 310px; 8 | height: 444px; 9 | z-index: 1; 10 | padding: 10px; 11 | border-radius: 15px; 12 | background-color: #ffffff; 13 | display: none; 14 | box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 10px 0px; 15 | } 16 | 17 | div.vis-color-picker div.vis-arrow { 18 | position: absolute; 19 | top: 147px; 20 | left: 5px; 21 | } 22 | 23 | div.vis-color-picker div.vis-arrow::after, 24 | div.vis-color-picker div.vis-arrow::before { 25 | right: 100%; 26 | top: 50%; 27 | border: solid transparent; 28 | content: " "; 29 | height: 0; 30 | width: 0; 31 | position: absolute; 32 | pointer-events: none; 33 | } 34 | 35 | div.vis-color-picker div.vis-arrow:after { 36 | border-color: rgba(255, 255, 255, 0); 37 | border-right-color: #ffffff; 38 | border-width: 30px; 39 | margin-top: -30px; 40 | } 41 | 42 | div.vis-color-picker div.vis-color { 43 | position: absolute; 44 | width: 289px; 45 | height: 289px; 46 | cursor: pointer; 47 | } 48 | 49 | div.vis-color-picker div.vis-brightness { 50 | position: absolute; 51 | top: 313px; 52 | } 53 | 54 | div.vis-color-picker div.vis-opacity { 55 | position: absolute; 56 | top: 350px; 57 | } 58 | 59 | div.vis-color-picker div.vis-selector { 60 | position: absolute; 61 | top: 137px; 62 | left: 137px; 63 | width: 15px; 64 | height: 15px; 65 | border-radius: 15px; 66 | border: 1px solid #ffffff; 67 | background: #4c4c4c; /* Old browsers */ 68 | background: -moz-linear-gradient( 69 | top, 70 | #4c4c4c 0%, 71 | #595959 12%, 72 | #666666 25%, 73 | #474747 39%, 74 | #2c2c2c 50%, 75 | #000000 51%, 76 | #111111 60%, 77 | #2b2b2b 76%, 78 | #1c1c1c 91%, 79 | #131313 100% 80 | ); /* FF3.6+ */ 81 | background: -webkit-gradient( 82 | linear, 83 | left top, 84 | left bottom, 85 | color-stop(0%, #4c4c4c), 86 | color-stop(12%, #595959), 87 | color-stop(25%, #666666), 88 | color-stop(39%, #474747), 89 | color-stop(50%, #2c2c2c), 90 | color-stop(51%, #000000), 91 | color-stop(60%, #111111), 92 | color-stop(76%, #2b2b2b), 93 | color-stop(91%, #1c1c1c), 94 | color-stop(100%, #131313) 95 | ); /* Chrome,Safari4+ */ 96 | background: -webkit-linear-gradient( 97 | top, 98 | #4c4c4c 0%, 99 | #595959 12%, 100 | #666666 25%, 101 | #474747 39%, 102 | #2c2c2c 50%, 103 | #000000 51%, 104 | #111111 60%, 105 | #2b2b2b 76%, 106 | #1c1c1c 91%, 107 | #131313 100% 108 | ); /* Chrome10+,Safari5.1+ */ 109 | background: -o-linear-gradient( 110 | top, 111 | #4c4c4c 0%, 112 | #595959 12%, 113 | #666666 25%, 114 | #474747 39%, 115 | #2c2c2c 50%, 116 | #000000 51%, 117 | #111111 60%, 118 | #2b2b2b 76%, 119 | #1c1c1c 91%, 120 | #131313 100% 121 | ); /* Opera 11.10+ */ 122 | background: -ms-linear-gradient( 123 | top, 124 | #4c4c4c 0%, 125 | #595959 12%, 126 | #666666 25%, 127 | #474747 39%, 128 | #2c2c2c 50%, 129 | #000000 51%, 130 | #111111 60%, 131 | #2b2b2b 76%, 132 | #1c1c1c 91%, 133 | #131313 100% 134 | ); /* IE10+ */ 135 | background: linear-gradient( 136 | to bottom, 137 | #4c4c4c 0%, 138 | #595959 12%, 139 | #666666 25%, 140 | #474747 39%, 141 | #2c2c2c 50%, 142 | #000000 51%, 143 | #111111 60%, 144 | #2b2b2b 76%, 145 | #1c1c1c 91%, 146 | #131313 100% 147 | ); /* W3C */ 148 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4c4c4c', endColorstr='#131313',GradientType=0 ); /* IE6-9 */ 149 | } 150 | 151 | div.vis-color-picker div.vis-new-color { 152 | position: absolute; 153 | width: 140px; 154 | height: 20px; 155 | border: 1px solid rgba(0, 0, 0, 0.1); 156 | border-radius: 5px; 157 | top: 380px; 158 | left: 159px; 159 | text-align: right; 160 | padding-right: 2px; 161 | font-size: 10px; 162 | color: rgba(0, 0, 0, 0.4); 163 | vertical-align: middle; 164 | line-height: 20px; 165 | } 166 | 167 | div.vis-color-picker div.vis-initial-color { 168 | position: absolute; 169 | width: 140px; 170 | height: 20px; 171 | border: 1px solid rgba(0, 0, 0, 0.1); 172 | border-radius: 5px; 173 | top: 380px; 174 | left: 10px; 175 | text-align: left; 176 | padding-left: 2px; 177 | font-size: 10px; 178 | color: rgba(0, 0, 0, 0.4); 179 | vertical-align: middle; 180 | line-height: 20px; 181 | } 182 | 183 | div.vis-color-picker div.vis-label { 184 | position: absolute; 185 | width: 300px; 186 | left: 10px; 187 | } 188 | 189 | div.vis-color-picker div.vis-label.vis-brightness { 190 | top: 300px; 191 | } 192 | 193 | div.vis-color-picker div.vis-label.vis-opacity { 194 | top: 338px; 195 | } 196 | 197 | div.vis-color-picker div.vis-button { 198 | position: absolute; 199 | width: 68px; 200 | height: 25px; 201 | border-radius: 10px; 202 | vertical-align: middle; 203 | text-align: center; 204 | line-height: 25px; 205 | top: 410px; 206 | border: 2px solid #d9d9d9; 207 | background-color: #f7f7f7; 208 | cursor: pointer; 209 | } 210 | 211 | div.vis-color-picker div.vis-button.vis-cancel { 212 | /*border:2px solid #ff4e33;*/ 213 | /*background-color: #ff7761;*/ 214 | left: 5px; 215 | } 216 | div.vis-color-picker div.vis-button.vis-load { 217 | /*border:2px solid #a153e6;*/ 218 | /*background-color: #cb8dff;*/ 219 | left: 82px; 220 | } 221 | div.vis-color-picker div.vis-button.vis-apply { 222 | /*border:2px solid #4588e6;*/ 223 | /*background-color: #82b6ff;*/ 224 | left: 159px; 225 | } 226 | div.vis-color-picker div.vis-button.vis-save { 227 | /*border:2px solid #45e655;*/ 228 | /*background-color: #6dff7c;*/ 229 | left: 236px; 230 | } 231 | 232 | div.vis-color-picker input.vis-range { 233 | width: 290px; 234 | height: 20px; 235 | } 236 | 237 | /* TODO: is this redundant? 238 | div.vis-color-picker input.vis-range-brightness { 239 | width: 289px !important; 240 | } 241 | 242 | 243 | div.vis-color-picker input.vis-saturation-range { 244 | width: 289px !important; 245 | }*/ 246 | -------------------------------------------------------------------------------- /src/shared/color-picker.d.ts: -------------------------------------------------------------------------------- 1 | export const ColorPicker: any; 2 | -------------------------------------------------------------------------------- /src/shared/configurator-types.ts: -------------------------------------------------------------------------------- 1 | interface OptionsType { 2 | readonly any?: "any"; 3 | readonly array?: "array"; 4 | readonly boolean?: "boolean"; 5 | readonly dom?: "dom"; 6 | readonly function?: "function"; 7 | readonly number?: "number"; 8 | readonly object?: "object"; 9 | readonly string?: "string" | readonly string[]; 10 | readonly undefined?: "undefined"; 11 | } 12 | export type OptionsConfig = { 13 | /* eslint-disable-next-line @typescript-eslint/naming-convention -- The __*__ format is used to prevent collisions with actual option names. */ 14 | readonly __type__: { 15 | readonly object: "object"; 16 | } & OptionsType; 17 | } & { 18 | readonly [Key in string]: OptionsConfig | OptionsType; 19 | }; 20 | 21 | /** 22 | * The value supplied will be used as the initial value. 23 | */ 24 | type CheckboxInput = boolean; 25 | /** 26 | * The passed text will be used as the initial value. Any text will be accepted 27 | * afterwards. 28 | */ 29 | type TextInput = string; 30 | /** 31 | * The passed values will be used as the limits and the initial position of a 32 | * slider. 33 | * 34 | * @remarks 35 | * ```typescript 36 | * readonly [ 37 | * initialValue: number, 38 | * min: number, 39 | * max: number, 40 | * step: number 41 | * ] 42 | * ``` 43 | */ 44 | type NumberInput = readonly [number, number, number, number]; 45 | /** 46 | * Translations for people with poor understanding of TypeScript: the first 47 | * value always has to be a string but never `"color"`, the rest can be any 48 | * combination of strings, numbers and booleans. 49 | */ 50 | type DropdownInput = readonly [ 51 | Exclude, 52 | ...(string | number | boolean)[] 53 | ]; 54 | /** 55 | * The first value says this will be a color picker not a dropdown menu. The 56 | * next value is the initial color. 57 | */ 58 | type ColorInput = readonly ["coor", string]; 59 | 60 | type ConfiguratorInput = 61 | | CheckboxInput 62 | | ColorInput 63 | | DropdownInput 64 | | NumberInput 65 | | TextInput; 66 | 67 | export type ConfiguratorConfig = { 68 | readonly [Key in string]: ConfiguratorConfig | ConfiguratorInput; 69 | }; 70 | 71 | export type ConfiguratorHideOption = ( 72 | parentPath: readonly string[], 73 | optionName: string, 74 | options: any 75 | ) => boolean; 76 | -------------------------------------------------------------------------------- /src/shared/configurator.css: -------------------------------------------------------------------------------- 1 | div.vis-configuration { 2 | position: relative; 3 | display: block; 4 | float: left; 5 | font-size: 12px; 6 | } 7 | 8 | div.vis-configuration-wrapper { 9 | display: block; 10 | width: 700px; 11 | } 12 | 13 | div.vis-configuration-wrapper::after { 14 | clear: both; 15 | content: ""; 16 | display: block; 17 | } 18 | 19 | div.vis-configuration.vis-config-option-container { 20 | display: block; 21 | width: 495px; 22 | background-color: #ffffff; 23 | border: 2px solid #f7f8fa; 24 | border-radius: 4px; 25 | margin-top: 20px; 26 | left: 10px; 27 | padding-left: 5px; 28 | } 29 | 30 | div.vis-configuration.vis-config-button { 31 | display: block; 32 | width: 495px; 33 | height: 25px; 34 | vertical-align: middle; 35 | line-height: 25px; 36 | background-color: #f7f8fa; 37 | border: 2px solid #ceced0; 38 | border-radius: 4px; 39 | margin-top: 20px; 40 | left: 10px; 41 | padding-left: 5px; 42 | cursor: pointer; 43 | margin-bottom: 30px; 44 | } 45 | 46 | div.vis-configuration.vis-config-button.hover { 47 | background-color: #4588e6; 48 | border: 2px solid #214373; 49 | color: #ffffff; 50 | } 51 | 52 | div.vis-configuration.vis-config-item { 53 | display: block; 54 | float: left; 55 | width: 495px; 56 | height: 25px; 57 | vertical-align: middle; 58 | line-height: 25px; 59 | } 60 | 61 | div.vis-configuration.vis-config-item.vis-config-s2 { 62 | left: 10px; 63 | background-color: #f7f8fa; 64 | padding-left: 5px; 65 | border-radius: 3px; 66 | } 67 | div.vis-configuration.vis-config-item.vis-config-s3 { 68 | left: 20px; 69 | background-color: #e4e9f0; 70 | padding-left: 5px; 71 | border-radius: 3px; 72 | } 73 | div.vis-configuration.vis-config-item.vis-config-s4 { 74 | left: 30px; 75 | background-color: #cfd8e6; 76 | padding-left: 5px; 77 | border-radius: 3px; 78 | } 79 | 80 | div.vis-configuration.vis-config-header { 81 | font-size: 18px; 82 | font-weight: bold; 83 | } 84 | 85 | div.vis-configuration.vis-config-label { 86 | width: 120px; 87 | height: 25px; 88 | line-height: 25px; 89 | } 90 | 91 | div.vis-configuration.vis-config-label.vis-config-s3 { 92 | width: 110px; 93 | } 94 | div.vis-configuration.vis-config-label.vis-config-s4 { 95 | width: 100px; 96 | } 97 | 98 | div.vis-configuration.vis-config-colorBlock { 99 | top: 1px; 100 | width: 30px; 101 | height: 19px; 102 | border: 1px solid #444444; 103 | border-radius: 2px; 104 | padding: 0px; 105 | margin: 0px; 106 | cursor: pointer; 107 | } 108 | 109 | input.vis-configuration.vis-config-checkbox { 110 | left: -5px; 111 | } 112 | 113 | input.vis-configuration.vis-config-rangeinput { 114 | position: relative; 115 | top: -5px; 116 | width: 60px; 117 | /*height:13px;*/ 118 | padding: 1px; 119 | margin: 0; 120 | pointer-events: none; 121 | } 122 | 123 | input.vis-configuration.vis-config-range { 124 | /*removes default webkit styles*/ 125 | -webkit-appearance: none; 126 | 127 | /*fix for FF unable to apply focus style bug */ 128 | border: 0px solid white; 129 | background-color: rgba(0, 0, 0, 0); 130 | 131 | /*required for proper track sizing in FF*/ 132 | width: 300px; 133 | height: 20px; 134 | } 135 | input.vis-configuration.vis-config-range::-webkit-slider-runnable-track { 136 | width: 300px; 137 | height: 5px; 138 | background: #dedede; /* Old browsers */ 139 | background: -moz-linear-gradient(top, #dedede 0%, #c8c8c8 99%); /* FF3.6+ */ 140 | background: -webkit-gradient( 141 | linear, 142 | left top, 143 | left bottom, 144 | color-stop(0%, #dedede), 145 | color-stop(99%, #c8c8c8) 146 | ); /* Chrome,Safari4+ */ 147 | background: -webkit-linear-gradient( 148 | top, 149 | #dedede 0%, 150 | #c8c8c8 99% 151 | ); /* Chrome10+,Safari5.1+ */ 152 | background: -o-linear-gradient( 153 | top, 154 | #dedede 0%, 155 | #c8c8c8 99% 156 | ); /* Opera 11.10+ */ 157 | background: -ms-linear-gradient(top, #dedede 0%, #c8c8c8 99%); /* IE10+ */ 158 | background: linear-gradient(to bottom, #dedede 0%, #c8c8c8 99%); /* W3C */ 159 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#dedede', endColorstr='#c8c8c8',GradientType=0 ); /* IE6-9 */ 160 | 161 | border: 1px solid #999999; 162 | box-shadow: #aaaaaa 0px 0px 3px 0px; 163 | border-radius: 3px; 164 | } 165 | input.vis-configuration.vis-config-range::-webkit-slider-thumb { 166 | -webkit-appearance: none; 167 | border: 1px solid #14334b; 168 | height: 17px; 169 | width: 17px; 170 | border-radius: 50%; 171 | background: #3876c2; /* Old browsers */ 172 | background: -moz-linear-gradient(top, #3876c2 0%, #385380 100%); /* FF3.6+ */ 173 | background: -webkit-gradient( 174 | linear, 175 | left top, 176 | left bottom, 177 | color-stop(0%, #3876c2), 178 | color-stop(100%, #385380) 179 | ); /* Chrome,Safari4+ */ 180 | background: -webkit-linear-gradient( 181 | top, 182 | #3876c2 0%, 183 | #385380 100% 184 | ); /* Chrome10+,Safari5.1+ */ 185 | background: -o-linear-gradient( 186 | top, 187 | #3876c2 0%, 188 | #385380 100% 189 | ); /* Opera 11.10+ */ 190 | background: -ms-linear-gradient(top, #3876c2 0%, #385380 100%); /* IE10+ */ 191 | background: linear-gradient(to bottom, #3876c2 0%, #385380 100%); /* W3C */ 192 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#3876c2', endColorstr='#385380',GradientType=0 ); /* IE6-9 */ 193 | box-shadow: #111927 0px 0px 1px 0px; 194 | margin-top: -7px; 195 | } 196 | input.vis-configuration.vis-config-range:focus { 197 | outline: none; 198 | } 199 | input.vis-configuration.vis-config-range:focus::-webkit-slider-runnable-track { 200 | background: #9d9d9d; /* Old browsers */ 201 | background: -moz-linear-gradient(top, #9d9d9d 0%, #c8c8c8 99%); /* FF3.6+ */ 202 | background: -webkit-gradient( 203 | linear, 204 | left top, 205 | left bottom, 206 | color-stop(0%, #9d9d9d), 207 | color-stop(99%, #c8c8c8) 208 | ); /* Chrome,Safari4+ */ 209 | background: -webkit-linear-gradient( 210 | top, 211 | #9d9d9d 0%, 212 | #c8c8c8 99% 213 | ); /* Chrome10+,Safari5.1+ */ 214 | background: -o-linear-gradient( 215 | top, 216 | #9d9d9d 0%, 217 | #c8c8c8 99% 218 | ); /* Opera 11.10+ */ 219 | background: -ms-linear-gradient(top, #9d9d9d 0%, #c8c8c8 99%); /* IE10+ */ 220 | background: linear-gradient(to bottom, #9d9d9d 0%, #c8c8c8 99%); /* W3C */ 221 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9d9d9d', endColorstr='#c8c8c8',GradientType=0 ); /* IE6-9 */ 222 | } 223 | 224 | input.vis-configuration.vis-config-range::-moz-range-track { 225 | width: 300px; 226 | height: 10px; 227 | background: #dedede; /* Old browsers */ 228 | background: -moz-linear-gradient(top, #dedede 0%, #c8c8c8 99%); /* FF3.6+ */ 229 | background: -webkit-gradient( 230 | linear, 231 | left top, 232 | left bottom, 233 | color-stop(0%, #dedede), 234 | color-stop(99%, #c8c8c8) 235 | ); /* Chrome,Safari4+ */ 236 | background: -webkit-linear-gradient( 237 | top, 238 | #dedede 0%, 239 | #c8c8c8 99% 240 | ); /* Chrome10+,Safari5.1+ */ 241 | background: -o-linear-gradient( 242 | top, 243 | #dedede 0%, 244 | #c8c8c8 99% 245 | ); /* Opera 11.10+ */ 246 | background: -ms-linear-gradient(top, #dedede 0%, #c8c8c8 99%); /* IE10+ */ 247 | background: linear-gradient(to bottom, #dedede 0%, #c8c8c8 99%); /* W3C */ 248 | filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#dedede', endColorstr='#c8c8c8',GradientType=0 ); /* IE6-9 */ 249 | 250 | border: 1px solid #999999; 251 | box-shadow: #aaaaaa 0px 0px 3px 0px; 252 | border-radius: 3px; 253 | } 254 | input.vis-configuration.vis-config-range::-moz-range-thumb { 255 | border: none; 256 | height: 16px; 257 | width: 16px; 258 | 259 | border-radius: 50%; 260 | background: #385380; 261 | } 262 | 263 | /*hide the outline behind the border*/ 264 | input.vis-configuration.vis-config-range:-moz-focusring { 265 | outline: 1px solid white; 266 | outline-offset: -1px; 267 | } 268 | 269 | input.vis-configuration.vis-config-range::-ms-track { 270 | width: 300px; 271 | height: 5px; 272 | 273 | /*remove bg colour from the track, we'll use ms-fill-lower and ms-fill-upper instead */ 274 | background: transparent; 275 | 276 | /*leave room for the larger thumb to overflow with a transparent border */ 277 | border-color: transparent; 278 | border-width: 6px 0; 279 | 280 | /*remove default tick marks*/ 281 | color: transparent; 282 | } 283 | input.vis-configuration.vis-config-range::-ms-fill-lower { 284 | background: #777; 285 | border-radius: 10px; 286 | } 287 | input.vis-configuration.vis-config-range::-ms-fill-upper { 288 | background: #ddd; 289 | border-radius: 10px; 290 | } 291 | input.vis-configuration.vis-config-range::-ms-thumb { 292 | border: none; 293 | height: 16px; 294 | width: 16px; 295 | border-radius: 50%; 296 | background: #385380; 297 | } 298 | input.vis-configuration.vis-config-range:focus::-ms-fill-lower { 299 | background: #888; 300 | } 301 | input.vis-configuration.vis-config-range:focus::-ms-fill-upper { 302 | background: #ccc; 303 | } 304 | 305 | .vis-configuration-popup { 306 | position: absolute; 307 | background: rgba(57, 76, 89, 0.85); 308 | border: 2px solid #f2faff; 309 | line-height: 30px; 310 | height: 30px; 311 | width: 150px; 312 | text-align: center; 313 | color: #ffffff; 314 | font-size: 14px; 315 | border-radius: 4px; 316 | -webkit-transition: opacity 0.3s ease-in-out; 317 | -moz-transition: opacity 0.3s ease-in-out; 318 | transition: opacity 0.3s ease-in-out; 319 | } 320 | .vis-configuration-popup:after, 321 | .vis-configuration-popup:before { 322 | left: 100%; 323 | top: 50%; 324 | border: solid transparent; 325 | content: " "; 326 | height: 0; 327 | width: 0; 328 | position: absolute; 329 | pointer-events: none; 330 | } 331 | 332 | .vis-configuration-popup:after { 333 | border-color: rgba(136, 183, 213, 0); 334 | border-left-color: rgba(57, 76, 89, 0.85); 335 | border-width: 8px; 336 | margin-top: -8px; 337 | } 338 | .vis-configuration-popup:before { 339 | border-color: rgba(194, 225, 245, 0); 340 | border-left-color: #f2faff; 341 | border-width: 12px; 342 | margin-top: -12px; 343 | } 344 | -------------------------------------------------------------------------------- /src/shared/configurator.d.ts: -------------------------------------------------------------------------------- 1 | export const Configurator: any; 2 | -------------------------------------------------------------------------------- /src/shared/hammer.d.ts: -------------------------------------------------------------------------------- 1 | export const Hammer: HammerStatic; 2 | -------------------------------------------------------------------------------- /src/shared/hammer.js: -------------------------------------------------------------------------------- 1 | import RealHammer from "@egjs/hammerjs"; 2 | 3 | /** 4 | * Setup a mock hammer.js object, for unit testing. 5 | * 6 | * Inspiration: https://github.com/uber/deck.gl/pull/658 7 | * 8 | * @returns {{on: noop, off: noop, destroy: noop, emit: noop, get: get}} 9 | */ 10 | function hammerMock() { 11 | const noop = () => {}; 12 | 13 | return { 14 | on: noop, 15 | off: noop, 16 | destroy: noop, 17 | emit: noop, 18 | 19 | get() { 20 | return { 21 | set: noop, 22 | }; 23 | }, 24 | }; 25 | } 26 | 27 | const Hammer = 28 | typeof window !== "undefined" 29 | ? window.Hammer || RealHammer 30 | : function () { 31 | // hammer.js is only available in a browser, not in node.js. Replacing it with a mock object. 32 | return hammerMock(); 33 | }; 34 | 35 | export { Hammer }; 36 | -------------------------------------------------------------------------------- /src/shared/index.ts: -------------------------------------------------------------------------------- 1 | import { Activator as ActivatorJS } from "./activator"; 2 | import { ColorPicker as ColorPickerJS } from "./color-picker"; 3 | import { Configurator as ConfiguratorJS } from "./configurator"; 4 | import { Hammer as HammerJS } from "./hammer"; 5 | import { Popup as PopupJS } from "./popup"; 6 | import { VALIDATOR_PRINT_STYLE as VALIDATOR_PRINT_STYLE_JS } from "./validator"; 7 | import { Validator as ValidatorJS } from "./validator"; 8 | 9 | export const Activator: any = ActivatorJS; 10 | export const ColorPicker: any = ColorPickerJS; 11 | export const Configurator: any = ConfiguratorJS; 12 | export const Hammer: HammerStatic = HammerJS; 13 | export const Popup: any = PopupJS; 14 | export const VALIDATOR_PRINT_STYLE: string = VALIDATOR_PRINT_STYLE_JS; 15 | export const Validator: any = ValidatorJS; 16 | 17 | export * from "./configurator-types"; 18 | -------------------------------------------------------------------------------- /src/shared/popup.css: -------------------------------------------------------------------------------- 1 | div.vis-tooltip { 2 | position: absolute; 3 | visibility: hidden; 4 | padding: 5px; 5 | white-space: nowrap; 6 | 7 | font-family: verdana; 8 | font-size: 14px; 9 | color: #000000; 10 | background-color: #f5f4ed; 11 | 12 | -moz-border-radius: 3px; 13 | -webkit-border-radius: 3px; 14 | border-radius: 3px; 15 | border: 1px solid #808074; 16 | 17 | box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2); 18 | pointer-events: none; 19 | 20 | z-index: 5; 21 | } 22 | -------------------------------------------------------------------------------- /src/shared/popup.d.ts: -------------------------------------------------------------------------------- 1 | export const Popup: any; 2 | -------------------------------------------------------------------------------- /src/shared/popup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Popup is a class to create a popup window with some text 3 | */ 4 | export class Popup { 5 | /** 6 | * @param {Element} container The container object. 7 | * @param {string} overflowMethod How the popup should act to overflowing ('flip' or 'cap') 8 | */ 9 | constructor(container, overflowMethod) { 10 | this.container = container; 11 | this.overflowMethod = overflowMethod || "cap"; 12 | 13 | this.x = 0; 14 | this.y = 0; 15 | this.padding = 5; 16 | this.hidden = false; 17 | 18 | // create the frame 19 | this.frame = document.createElement("div"); 20 | this.frame.className = "vis-tooltip"; 21 | this.container.appendChild(this.frame); 22 | } 23 | 24 | /** 25 | * @param {number} x Horizontal position of the popup window 26 | * @param {number} y Vertical position of the popup window 27 | */ 28 | setPosition(x, y) { 29 | this.x = parseInt(x); 30 | this.y = parseInt(y); 31 | } 32 | 33 | /** 34 | * Set the content for the popup window. This can be HTML code or text. 35 | * 36 | * @param {string | Element} content 37 | */ 38 | setText(content) { 39 | if (content instanceof Element) { 40 | while (this.frame.firstChild) { 41 | this.frame.removeChild(this.frame.firstChild); 42 | } 43 | this.frame.appendChild(content); 44 | } else { 45 | // String containing literal text, element has to be used for HTML due to 46 | // XSS risks associated with innerHTML (i.e. prevent XSS by accident). 47 | this.frame.innerText = content; 48 | } 49 | } 50 | 51 | /** 52 | * Show the popup window 53 | * 54 | * @param {boolean} [doShow] Show or hide the window 55 | */ 56 | show(doShow) { 57 | if (doShow === undefined) { 58 | doShow = true; 59 | } 60 | 61 | if (doShow === true) { 62 | const height = this.frame.clientHeight; 63 | const width = this.frame.clientWidth; 64 | const maxHeight = this.frame.parentNode.clientHeight; 65 | const maxWidth = this.frame.parentNode.clientWidth; 66 | 67 | let left = 0, 68 | top = 0; 69 | 70 | if (this.overflowMethod == "flip") { 71 | let isLeft = false, 72 | isTop = true; // Where around the position it's located 73 | 74 | if (this.y - height < this.padding) { 75 | isTop = false; 76 | } 77 | 78 | if (this.x + width > maxWidth - this.padding) { 79 | isLeft = true; 80 | } 81 | 82 | if (isLeft) { 83 | left = this.x - width; 84 | } else { 85 | left = this.x; 86 | } 87 | 88 | if (isTop) { 89 | top = this.y - height; 90 | } else { 91 | top = this.y; 92 | } 93 | } else { 94 | top = this.y - height; 95 | if (top + height + this.padding > maxHeight) { 96 | top = maxHeight - height - this.padding; 97 | } 98 | if (top < this.padding) { 99 | top = this.padding; 100 | } 101 | 102 | left = this.x; 103 | if (left + width + this.padding > maxWidth) { 104 | left = maxWidth - width - this.padding; 105 | } 106 | if (left < this.padding) { 107 | left = this.padding; 108 | } 109 | } 110 | 111 | this.frame.style.left = left + "px"; 112 | this.frame.style.top = top + "px"; 113 | this.frame.style.visibility = "visible"; 114 | this.hidden = false; 115 | } else { 116 | this.hide(); 117 | } 118 | } 119 | 120 | /** 121 | * Hide the popup window 122 | */ 123 | hide() { 124 | this.hidden = true; 125 | this.frame.style.left = "0"; 126 | this.frame.style.top = "0"; 127 | this.frame.style.visibility = "hidden"; 128 | } 129 | 130 | /** 131 | * Remove the popup window 132 | */ 133 | destroy() { 134 | this.frame.parentNode.removeChild(this.frame); // Remove element from DOM 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/shared/validator.d.ts: -------------------------------------------------------------------------------- 1 | export const VALIDATOR_PRINT_STYLE: any; 2 | export const Validator: any; 3 | -------------------------------------------------------------------------------- /src/shared/validator.js: -------------------------------------------------------------------------------- 1 | import { copyAndExtendArray, copyArray } from "../util"; 2 | 3 | let errorFound = false; 4 | let allOptions; 5 | 6 | export const VALIDATOR_PRINT_STYLE = "background: #FFeeee; color: #dd0000"; 7 | 8 | /** 9 | * Used to validate options. 10 | */ 11 | export class Validator { 12 | /** 13 | * Main function to be called 14 | * 15 | * @param {object} options 16 | * @param {object} referenceOptions 17 | * @param {object} subObject 18 | * @returns {boolean} 19 | * @static 20 | */ 21 | static validate(options, referenceOptions, subObject) { 22 | errorFound = false; 23 | allOptions = referenceOptions; 24 | let usedOptions = referenceOptions; 25 | if (subObject !== undefined) { 26 | usedOptions = referenceOptions[subObject]; 27 | } 28 | Validator.parse(options, usedOptions, []); 29 | return errorFound; 30 | } 31 | 32 | /** 33 | * Will traverse an object recursively and check every value 34 | * 35 | * @param {object} options 36 | * @param {object} referenceOptions 37 | * @param {Array} path | where to look for the actual option 38 | * @static 39 | */ 40 | static parse(options, referenceOptions, path) { 41 | for (const option in options) { 42 | if (Object.prototype.hasOwnProperty.call(options, option)) { 43 | Validator.check(option, options, referenceOptions, path); 44 | } 45 | } 46 | } 47 | 48 | /** 49 | * Check every value. If the value is an object, call the parse function on that object. 50 | * 51 | * @param {string} option 52 | * @param {object} options 53 | * @param {object} referenceOptions 54 | * @param {Array} path | where to look for the actual option 55 | * @static 56 | */ 57 | static check(option, options, referenceOptions, path) { 58 | if ( 59 | referenceOptions[option] === undefined && 60 | referenceOptions.__any__ === undefined 61 | ) { 62 | Validator.getSuggestion(option, referenceOptions, path); 63 | return; 64 | } 65 | 66 | let referenceOption = option; 67 | let is_object = true; 68 | 69 | if ( 70 | referenceOptions[option] === undefined && 71 | referenceOptions.__any__ !== undefined 72 | ) { 73 | // NOTE: This only triggers if the __any__ is in the top level of the options object. 74 | // THAT'S A REALLY BAD PLACE TO ALLOW IT!!!! 75 | // TODO: Examine if needed, remove if possible 76 | 77 | // __any__ is a wildcard. Any value is accepted and will be further analysed by reference. 78 | referenceOption = "__any__"; 79 | 80 | // if the any-subgroup is not a predefined object in the configurator, 81 | // we do not look deeper into the object. 82 | is_object = Validator.getType(options[option]) === "object"; 83 | } else { 84 | // Since all options in the reference are objects, we can check whether 85 | // they are supposed to be the object to look for the __type__ field. 86 | // if this is an object, we check if the correct type has been supplied to account for shorthand options. 87 | } 88 | 89 | let refOptionObj = referenceOptions[referenceOption]; 90 | if (is_object && refOptionObj.__type__ !== undefined) { 91 | refOptionObj = refOptionObj.__type__; 92 | } 93 | 94 | Validator.checkFields( 95 | option, 96 | options, 97 | referenceOptions, 98 | referenceOption, 99 | refOptionObj, 100 | path 101 | ); 102 | } 103 | 104 | /** 105 | * 106 | * @param {string} option | the option property 107 | * @param {object} options | The supplied options object 108 | * @param {object} referenceOptions | The reference options containing all options and their allowed formats 109 | * @param {string} referenceOption | Usually this is the same as option, except when handling an __any__ tag. 110 | * @param {string} refOptionObj | This is the type object from the reference options 111 | * @param {Array} path | where in the object is the option 112 | * @static 113 | */ 114 | static checkFields( 115 | option, 116 | options, 117 | referenceOptions, 118 | referenceOption, 119 | refOptionObj, 120 | path 121 | ) { 122 | const log = function (message) { 123 | console.error( 124 | "%c" + message + Validator.printLocation(path, option), 125 | VALIDATOR_PRINT_STYLE 126 | ); 127 | }; 128 | 129 | const optionType = Validator.getType(options[option]); 130 | const refOptionType = refOptionObj[optionType]; 131 | 132 | if (refOptionType !== undefined) { 133 | // if the type is correct, we check if it is supposed to be one of a few select values 134 | if ( 135 | Validator.getType(refOptionType) === "array" && 136 | refOptionType.indexOf(options[option]) === -1 137 | ) { 138 | log( 139 | 'Invalid option detected in "' + 140 | option + 141 | '".' + 142 | " Allowed values are:" + 143 | Validator.print(refOptionType) + 144 | ' not "' + 145 | options[option] + 146 | '". ' 147 | ); 148 | errorFound = true; 149 | } else if (optionType === "object" && referenceOption !== "__any__") { 150 | path = copyAndExtendArray(path, option); 151 | Validator.parse( 152 | options[option], 153 | referenceOptions[referenceOption], 154 | path 155 | ); 156 | } 157 | } else if (refOptionObj["any"] === undefined) { 158 | // type of the field is incorrect and the field cannot be any 159 | log( 160 | 'Invalid type received for "' + 161 | option + 162 | '". Expected: ' + 163 | Validator.print(Object.keys(refOptionObj)) + 164 | ". Received [" + 165 | optionType + 166 | '] "' + 167 | options[option] + 168 | '"' 169 | ); 170 | errorFound = true; 171 | } 172 | } 173 | 174 | /** 175 | * 176 | * @param {object | boolean | number | string | Array. | Date | Node | Moment | undefined | null} object 177 | * @returns {string} 178 | * @static 179 | */ 180 | static getType(object) { 181 | const type = typeof object; 182 | 183 | if (type === "object") { 184 | if (object === null) { 185 | return "null"; 186 | } 187 | if (object instanceof Boolean) { 188 | return "boolean"; 189 | } 190 | if (object instanceof Number) { 191 | return "number"; 192 | } 193 | if (object instanceof String) { 194 | return "string"; 195 | } 196 | if (Array.isArray(object)) { 197 | return "array"; 198 | } 199 | if (object instanceof Date) { 200 | return "date"; 201 | } 202 | if (object.nodeType !== undefined) { 203 | return "dom"; 204 | } 205 | if (object._isAMomentObject === true) { 206 | return "moment"; 207 | } 208 | return "object"; 209 | } else if (type === "number") { 210 | return "number"; 211 | } else if (type === "boolean") { 212 | return "boolean"; 213 | } else if (type === "string") { 214 | return "string"; 215 | } else if (type === undefined) { 216 | return "undefined"; 217 | } 218 | return type; 219 | } 220 | 221 | /** 222 | * @param {string} option 223 | * @param {object} options 224 | * @param {Array.} path 225 | * @static 226 | */ 227 | static getSuggestion(option, options, path) { 228 | const localSearch = Validator.findInOptions(option, options, path, false); 229 | const globalSearch = Validator.findInOptions(option, allOptions, [], true); 230 | 231 | const localSearchThreshold = 8; 232 | const globalSearchThreshold = 4; 233 | 234 | let msg; 235 | if (localSearch.indexMatch !== undefined) { 236 | msg = 237 | " in " + 238 | Validator.printLocation(localSearch.path, option, "") + 239 | 'Perhaps it was incomplete? Did you mean: "' + 240 | localSearch.indexMatch + 241 | '"?\n\n'; 242 | } else if ( 243 | globalSearch.distance <= globalSearchThreshold && 244 | localSearch.distance > globalSearch.distance 245 | ) { 246 | msg = 247 | " in " + 248 | Validator.printLocation(localSearch.path, option, "") + 249 | "Perhaps it was misplaced? Matching option found at: " + 250 | Validator.printLocation( 251 | globalSearch.path, 252 | globalSearch.closestMatch, 253 | "" 254 | ); 255 | } else if (localSearch.distance <= localSearchThreshold) { 256 | msg = 257 | '. Did you mean "' + 258 | localSearch.closestMatch + 259 | '"?' + 260 | Validator.printLocation(localSearch.path, option); 261 | } else { 262 | msg = 263 | ". Did you mean one of these: " + 264 | Validator.print(Object.keys(options)) + 265 | Validator.printLocation(path, option); 266 | } 267 | 268 | console.error( 269 | '%cUnknown option detected: "' + option + '"' + msg, 270 | VALIDATOR_PRINT_STYLE 271 | ); 272 | errorFound = true; 273 | } 274 | 275 | /** 276 | * traverse the options in search for a match. 277 | * 278 | * @param {string} option 279 | * @param {object} options 280 | * @param {Array} path | where to look for the actual option 281 | * @param {boolean} [recursive=false] 282 | * @returns {{closestMatch: string, path: Array, distance: number}} 283 | * @static 284 | */ 285 | static findInOptions(option, options, path, recursive = false) { 286 | let min = 1e9; 287 | let closestMatch = ""; 288 | let closestMatchPath = []; 289 | const lowerCaseOption = option.toLowerCase(); 290 | let indexMatch = undefined; 291 | for (const op in options) { 292 | let distance; 293 | if (options[op].__type__ !== undefined && recursive === true) { 294 | const result = Validator.findInOptions( 295 | option, 296 | options[op], 297 | copyAndExtendArray(path, op) 298 | ); 299 | if (min > result.distance) { 300 | closestMatch = result.closestMatch; 301 | closestMatchPath = result.path; 302 | min = result.distance; 303 | indexMatch = result.indexMatch; 304 | } 305 | } else { 306 | if (op.toLowerCase().indexOf(lowerCaseOption) !== -1) { 307 | indexMatch = op; 308 | } 309 | distance = Validator.levenshteinDistance(option, op); 310 | if (min > distance) { 311 | closestMatch = op; 312 | closestMatchPath = copyArray(path); 313 | min = distance; 314 | } 315 | } 316 | } 317 | return { 318 | closestMatch: closestMatch, 319 | path: closestMatchPath, 320 | distance: min, 321 | indexMatch: indexMatch, 322 | }; 323 | } 324 | 325 | /** 326 | * @param {Array.} path 327 | * @param {object} option 328 | * @param {string} prefix 329 | * @returns {string} 330 | * @static 331 | */ 332 | static printLocation(path, option, prefix = "Problem value found at: \n") { 333 | let str = "\n\n" + prefix + "options = {\n"; 334 | for (let i = 0; i < path.length; i++) { 335 | for (let j = 0; j < i + 1; j++) { 336 | str += " "; 337 | } 338 | str += path[i] + ": {\n"; 339 | } 340 | for (let j = 0; j < path.length + 1; j++) { 341 | str += " "; 342 | } 343 | str += option + "\n"; 344 | for (let i = 0; i < path.length + 1; i++) { 345 | for (let j = 0; j < path.length - i; j++) { 346 | str += " "; 347 | } 348 | str += "}\n"; 349 | } 350 | return str + "\n\n"; 351 | } 352 | 353 | /** 354 | * @param {object} options 355 | * @returns {string} 356 | * @static 357 | */ 358 | static print(options) { 359 | return JSON.stringify(options) 360 | .replace(/(")|(\[)|(\])|(,"__type__")/g, "") 361 | .replace(/(,)/g, ", "); 362 | } 363 | 364 | /** 365 | * Compute the edit distance between the two given strings 366 | * http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript 367 | * 368 | * Copyright (c) 2011 Andrei Mackenzie 369 | * 370 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 371 | * 372 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 373 | * 374 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 375 | * 376 | * @param {string} a 377 | * @param {string} b 378 | * @returns {Array.>}} 379 | * @static 380 | */ 381 | static levenshteinDistance(a, b) { 382 | if (a.length === 0) return b.length; 383 | if (b.length === 0) return a.length; 384 | 385 | const matrix = []; 386 | 387 | // increment along the first column of each row 388 | let i; 389 | for (i = 0; i <= b.length; i++) { 390 | matrix[i] = [i]; 391 | } 392 | 393 | // increment each column in the first row 394 | let j; 395 | for (j = 0; j <= a.length; j++) { 396 | matrix[0][j] = j; 397 | } 398 | 399 | // Fill in the rest of the matrix 400 | for (i = 1; i <= b.length; i++) { 401 | for (j = 1; j <= a.length; j++) { 402 | if (b.charAt(i - 1) == a.charAt(j - 1)) { 403 | matrix[i][j] = matrix[i - 1][j - 1]; 404 | } else { 405 | matrix[i][j] = Math.min( 406 | matrix[i - 1][j - 1] + 1, // substitution 407 | Math.min( 408 | matrix[i][j - 1] + 1, // insertion 409 | matrix[i - 1][j] + 1 410 | ) 411 | ); // deletion 412 | } 413 | } 414 | } 415 | 416 | return matrix[b.length][a.length]; 417 | } 418 | } 419 | -------------------------------------------------------------------------------- /test/add-class-name.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { addClassName } from "../src"; 4 | 5 | describe("addClassName", function (): void { 6 | const inputs: { input: string; classes: string; expected: string }[] = [ 7 | { input: "a b c", classes: "a c", expected: "a b c" }, 8 | { input: "a b c", classes: "d", expected: "a b c d" }, 9 | { input: "c", classes: "a b", expected: "c a b" }, 10 | { 11 | input: "class-1 class-2", 12 | classes: "class-3", 13 | expected: "class-1 class-2 class-3", 14 | }, 15 | ]; 16 | 17 | inputs.forEach(({ input, classes, expected }): void => { 18 | it(`${input} + ${classes} = ${expected}`, function (): void { 19 | const elem = { className: input }; 20 | 21 | addClassName(elem as any, classes); 22 | 23 | expect(elem.className).to.equal(expected); 24 | }); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/add-css-text.test.ts: -------------------------------------------------------------------------------- 1 | import jsdom_global from "jsdom-global"; 2 | import { expect } from "chai"; 3 | 4 | import { addCssText } from "../src"; 5 | 6 | describe("addCssText", function (): void { 7 | beforeEach(function () { 8 | this.jsdom_global = jsdom_global(); 9 | }); 10 | 11 | afterEach(function () { 12 | this.jsdom_global(); 13 | }); 14 | 15 | it("Add color and background URL to nothing", function (): void { 16 | const element = document.createElement("div"); 17 | element.style.cssText = ""; 18 | 19 | addCssText( 20 | element, 21 | "color: blue; background: url(http://www.example.com:8080/b.jpg);" 22 | ); 23 | 24 | expect(element.style.cssText).to.equal( 25 | "color: blue; background: url(http://www.example.com:8080/b.jpg);" 26 | ); 27 | }); 28 | 29 | it("Add nothing to color, margin and background URL", function (): void { 30 | const element = document.createElement("div"); 31 | element.style.cssText = 32 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 33 | 34 | addCssText(element, ""); 35 | 36 | expect(element.style.cssText).to.equal( 37 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);" 38 | ); 39 | }); 40 | 41 | it("Add padding to color, margin and background URL", function (): void { 42 | const element = document.createElement("div"); 43 | element.style.cssText = 44 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 45 | 46 | addCssText(element, "padding: 4ex;"); 47 | 48 | expect(element.style.cssText).to.equal( 49 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg); padding: 4ex;" 50 | ); 51 | }); 52 | 53 | it("Add color and background URL to color, margin and background URL", function (): void { 54 | const element = document.createElement("div"); 55 | element.style.cssText = 56 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 57 | 58 | addCssText( 59 | element, 60 | "color: blue; background: url(http://www.example.com:8080/b.jpg);" 61 | ); 62 | 63 | expect(element.style.cssText).to.equal( 64 | "color: blue; margin: 1em; background: url(http://www.example.com:8080/b.jpg);" 65 | ); 66 | }); 67 | }); 68 | -------------------------------------------------------------------------------- /test/binary-search-custom.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { assert, mock } from "sinon"; 3 | import { deepFreeze } from "./helpers"; 4 | 5 | import { binarySearchCustom } from "../src"; 6 | 7 | describe("binarySearchCustom", function (): void { 8 | const orderedItems = deepFreeze([ 9 | { prop1: -79, nested: { prop2: -0.79 } }, 10 | { prop1: -27, nested: { prop2: -0.27 } }, 11 | { prop1: -18, nested: { prop2: -0.18 } }, 12 | { prop1: -11, nested: { prop2: -0.11 } }, 13 | { prop1: 4, nested: { prop2: 0.4 } }, 14 | { prop1: 18, nested: { prop2: 0.18 } }, 15 | { prop1: 28, nested: { prop2: 0.28 } }, 16 | { prop1: 33, nested: { prop2: 0.33 } }, 17 | { prop1: 37, nested: { prop2: 0.37 } }, 18 | { prop1: 47, nested: { prop2: 0.47 } }, 19 | { prop1: 64, nested: { prop2: 0.64 } }, 20 | { prop1: 71, nested: { prop2: 0.71 } }, 21 | { prop1: 71, nested: { prop2: 0.71 } }, 22 | { prop1: 71, nested: { prop2: 0.71 } }, 23 | { prop1: 71, nested: { prop2: 0.71 } }, 24 | { prop1: 87, nested: { prop2: 0.87 } }, 25 | ]); 26 | const comparatorFactory = 27 | (target: number): ((value: number) => -1 | 0 | 1) => 28 | (value: number): -1 | 0 | 1 => { 29 | if (value < target) { 30 | return -1; 31 | } else if (value > target) { 32 | return 1; 33 | } else { 34 | return 0; 35 | } 36 | }; 37 | 38 | describe("array[index][prop1]", function (): void { 39 | it("comparator args", function (): void { 40 | const bscMock = mock(); 41 | bscMock.returns(0); 42 | 43 | binarySearchCustom(orderedItems, bscMock, "prop1"); 44 | 45 | assert.calledOnce(bscMock); 46 | expect( 47 | bscMock.getCall(0).args, 48 | "The comparator should receive the value retrieved from the array item using the passed property name." 49 | ) 50 | .to.have.lengthOf(1) 51 | .and.to.have.ownProperty("0") 52 | .that.is.a("number"); 53 | }); 54 | 55 | it("missing item", function (): void { 56 | expect( 57 | binarySearchCustom(orderedItems, comparatorFactory(13), "prop1") 58 | ).to.equal(-1); 59 | }); 60 | 61 | it("present item", function (): void { 62 | expect( 63 | binarySearchCustom(orderedItems, comparatorFactory(28), "prop1") 64 | ).to.equal(6); 65 | }); 66 | 67 | it("multiple equal items", function (): void { 68 | expect(binarySearchCustom(orderedItems, comparatorFactory(71), "prop1")) 69 | .to.be.at.least(11) 70 | .and.at.most(14); 71 | }); 72 | }); 73 | 74 | describe("array[index][prop1][prop2]", function (): void { 75 | it("comparator args", function (): void { 76 | const bscMock = mock(); 77 | bscMock.returns(0); 78 | 79 | binarySearchCustom(orderedItems, bscMock, "nested", "prop2"); 80 | 81 | assert.calledOnce(bscMock); 82 | expect( 83 | bscMock.getCall(0).args, 84 | "The comparator should receive the value retrieved from the array item using the passed property names." 85 | ) 86 | .to.have.lengthOf(1) 87 | .and.to.have.ownProperty("0") 88 | .that.is.a("number"); 89 | }); 90 | 91 | it("missing item", function (): void { 92 | expect( 93 | binarySearchCustom( 94 | orderedItems, 95 | comparatorFactory(0.13), 96 | "nested", 97 | "prop2" 98 | ) 99 | ).to.equal(-1); 100 | }); 101 | 102 | it("present item", function (): void { 103 | expect( 104 | binarySearchCustom( 105 | orderedItems, 106 | comparatorFactory(0.28), 107 | "nested", 108 | "prop2" 109 | ) 110 | ).to.equal(6); 111 | }); 112 | 113 | it("multiple equal items", function (): void { 114 | expect( 115 | binarySearchCustom( 116 | orderedItems, 117 | comparatorFactory(0.71), 118 | "nested", 119 | "prop2" 120 | ) 121 | ) 122 | .to.be.at.least(11) 123 | .and.at.most(14); 124 | }); 125 | }); 126 | }); 127 | -------------------------------------------------------------------------------- /test/bridge-object.test.ts: -------------------------------------------------------------------------------- 1 | import jsdom_global from "jsdom-global"; 2 | import { expect } from "chai"; 3 | 4 | import { bridgeObject } from "../src"; 5 | 6 | describe("bridgeObject", function (): void { 7 | beforeEach(function () { 8 | this.jsdom_global = jsdom_global(); 9 | }); 10 | 11 | afterEach(function () { 12 | this.jsdom_global(); 13 | }); 14 | 15 | describe("Invalid input", function (): void { 16 | [ 17 | null, 18 | 0, 19 | 7, 20 | true, 21 | false, 22 | Symbol("This just shouldn‘t work."), 23 | Number.POSITIVE_INFINITY, 24 | Number.NaN, 25 | "", 26 | "Have a nice day!", 27 | ].forEach((input): void => { 28 | it( 29 | typeof input === "symbol" ? "Symbol" : JSON.stringify(input), 30 | function (): void { 31 | expect( 32 | bridgeObject(input), 33 | "Null should be returned for invalid input." 34 | ).to.be.null; 35 | } 36 | ); 37 | }); 38 | }); 39 | 40 | describe("Valid input", function (): void { 41 | // @TODO: Set up DOM objects for testing. 42 | 43 | describe("Empty object", function (): void { 44 | const object = {}; 45 | let bridgedObject: {}; 46 | 47 | it("Bridge the object", function (): void { 48 | bridgedObject = bridgeObject(object); 49 | }); 50 | 51 | it("Bridged object shouldn‘t equal the original", function (): void { 52 | expect(bridgedObject).to.not.equal(object); 53 | }); 54 | 55 | it("Bridged object should have the original as it‘s prototype", function (): void { 56 | expect(Object.getPrototypeOf(bridgedObject)).to.equal(object); 57 | }); 58 | 59 | it("Bridged object should have no additional properies", function (): void { 60 | expect(Object.keys(bridgedObject)).to.be.empty; 61 | }); 62 | }); 63 | 64 | describe("Nested objects", function (): void { 65 | const o1 = {}; 66 | const o2 = {}; 67 | const o3 = {}; 68 | const o4 = {}; 69 | 70 | const object = { 71 | 1: o1, 72 | 2: { 2: o2 }, 73 | 3: { 3: { 3: o3 } }, 74 | 4: { 4: { 4: { 4: o4 } } }, 75 | }; 76 | let bridgedObject: typeof object; 77 | 78 | it("Bridge the object", function (): void { 79 | bridgedObject = bridgeObject(object); 80 | }); 81 | 82 | it("Bridged object shouldn‘t equal the original", function (): void { 83 | expect(bridgedObject).to.not.equal(object); 84 | }); 85 | 86 | it("Bridged object should have the original as it‘s prototype", function (): void { 87 | expect(Object.getPrototypeOf(bridgedObject)).to.equal(object); 88 | }); 89 | 90 | it("Bridged object should have all original properties but no additional", function (): void { 91 | expect(Object.keys(bridgedObject).sort()).to.deep.equal([ 92 | "1", 93 | "2", 94 | "3", 95 | "4", 96 | ]); 97 | }); 98 | 99 | describe("Objects should be deeply bridged", function (): void { 100 | it("#1", function (): void { 101 | expect(bridgedObject[1]).to.not.equal(o1); 102 | expect(Object.getPrototypeOf(bridgedObject[1])).to.equal(o1); 103 | }); 104 | 105 | it("#2", function (): void { 106 | expect(bridgedObject[2][2]).to.not.equal(o2); 107 | expect(Object.getPrototypeOf(bridgedObject[2][2])).to.equal(o2); 108 | }); 109 | 110 | it("#3", function (): void { 111 | expect(bridgedObject[3][3][3]).to.not.equal(o3); 112 | expect(Object.getPrototypeOf(bridgedObject[3][3][3])).to.equal(o3); 113 | }); 114 | 115 | it("#4", function (): void { 116 | expect(bridgedObject[4][4][4][4]).to.not.equal(o4); 117 | expect(Object.getPrototypeOf(bridgedObject[4][4][4][4])).to.equal(o4); 118 | }); 119 | }); 120 | }); 121 | }); 122 | }); 123 | -------------------------------------------------------------------------------- /test/copy-and-extend-array.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { copyAndExtendArray } from "../src"; 4 | 5 | describe("copyAndExtendArray", function (): void { 6 | it("number[]", function (): void { 7 | const original = Object.freeze([-77, 14, 78]); 8 | const addition = Object.freeze(0); 9 | const copied = copyAndExtendArray(original, addition); 10 | 11 | expect(copied, "They should be different instances.").to.not.equal( 12 | original 13 | ); 14 | expect( 15 | copied, 16 | "All elements should be in the new array and in the right order." 17 | ).to.deep.equal([-77, 14, 78, 0]); 18 | }); 19 | 20 | it("object[]", function (): void { 21 | const original = Object.freeze([ 22 | Object.freeze({ hi: ":-)" }), 23 | Object.freeze({ bye: ":-(" }), 24 | ]); 25 | const addition = Object.freeze({ hello: "☺" }); 26 | const copied = copyAndExtendArray(original, addition); 27 | 28 | expect(copied, "They should be different instances.").to.not.equal( 29 | original 30 | ); 31 | 32 | expect(copied[0], "The objects should be copied by reference").to.equal( 33 | original[0] 34 | ); 35 | expect(copied[1], "The objects should be copied by reference").to.equal( 36 | original[1] 37 | ); 38 | expect(copied[2], "The objects should be copied by reference").to.equal( 39 | addition 40 | ); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /test/copy-array.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { copyArray } from "../src"; 4 | 5 | describe("copyArray", function (): void { 6 | it("number[]", function (): void { 7 | const original = Object.freeze([-77, 14, 78]); 8 | const copied = copyArray(original); 9 | 10 | expect(copied, "Their content should be the same.").to.deep.equal(original); 11 | expect(copied, "They should be different instances.").to.not.equal( 12 | original 13 | ); 14 | }); 15 | 16 | it("object[]", function (): void { 17 | const original = Object.freeze([ 18 | Object.freeze({ hi: ":-)" }), 19 | Object.freeze({ bye: ":-(" }), 20 | ]); 21 | const copied = copyArray(original); 22 | 23 | expect(copied, "They should be different instances.").to.not.equal( 24 | original 25 | ); 26 | expect(copied[0], "The objects should be copied by reference").to.equal( 27 | original[0] 28 | ); 29 | expect(copied[1], "The objects should be copied by reference").to.equal( 30 | original[1] 31 | ); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /test/deep-extend.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { deepFreeze } from "./helpers"; 3 | 4 | import { deepExtend } from "../src"; 5 | 6 | describe("deepExtend", function (): void { 7 | it("nested strings", function (): void { 8 | const target = { 9 | p1: "p1 T", 10 | p2: "p2 T", 11 | p4: { 12 | p4p1: "p4p1 T", 13 | p4p2: "p4p2 T", 14 | p4p4: { 15 | p4p4p1: "p4p4p1 T", 16 | p4p4p2: "p4p4p2 T", 17 | }, 18 | }, 19 | }; 20 | const source = deepFreeze({ 21 | p2: "p2 S", 22 | p3: "p3 S", 23 | p4: { 24 | p4p2: "p4p2 S", 25 | p4p3: "p4p3 S", 26 | p4p4: { 27 | p4p4p2: "p4p4p2 S", 28 | p4p4p3: "p4p4p3 S", 29 | }, 30 | }, 31 | }); 32 | 33 | const merged = deepExtend(target, source); 34 | 35 | expect(merged, "They should be the same instance.").to.equal(target); 36 | expect(merged, "All the properties should be deeply merged.").to.deep.equal( 37 | { 38 | p1: "p1 T", 39 | p2: "p2 S", 40 | p3: "p3 S", 41 | p4: { 42 | p4p1: "p4p1 T", 43 | p4p2: "p4p2 S", 44 | p4p3: "p4p3 S", 45 | p4p4: { 46 | p4p4p1: "p4p4p1 T", 47 | p4p4p2: "p4p4p2 S", 48 | p4p4p3: "p4p4p3 S", 49 | }, 50 | }, 51 | } 52 | ); 53 | }); 54 | 55 | it("arrays", function (): void { 56 | const target = { 57 | arrays: { 58 | p1: ["T", 1, true, "T"], 59 | p2: ["T", 1, true, "T"], 60 | }, 61 | }; 62 | const source = deepFreeze({ 63 | arrays: { 64 | p2: ["S", false, 0, "S"], 65 | p3: ["S", false, 0, "S"], 66 | }, 67 | }); 68 | 69 | const merged = deepExtend(target, source); 70 | 71 | expect(merged, "They should be the same instance.").to.equal(target); 72 | expect( 73 | merged, 74 | "Objects inheriting directly from Object should be deeply merged, arrays replaced." 75 | ).to.deep.equal({ 76 | arrays: { 77 | p1: ["T", 1, true, "T"], 78 | p2: ["S", false, 0, "S"], 79 | p3: ["S", false, 0, "S"], 80 | }, 81 | }); 82 | 83 | expect( 84 | merged.arrays.p2, 85 | "Array should not be copied by reference." 86 | ).to.not.equal(source.arrays.p2); 87 | expect( 88 | merged.arrays.p2, 89 | "Array should not be copied by reference." 90 | ).to.not.equal(source.arrays.p2); 91 | }); 92 | 93 | it("objects with other than Object prototype", function (): void { 94 | const objectLiteral = { p3: "S" }; 95 | const objectFromNull = Object.create(null); 96 | const objectFromObject = Object.create(Object); 97 | const objectFromMap = new Map(); 98 | 99 | const target = { 100 | objects: { 101 | objectLiteral: { p1: "T" }, 102 | }, 103 | }; 104 | const source = deepFreeze({ 105 | objects: { 106 | objectLiteral, 107 | objectFromNull, 108 | objectFromObject, 109 | objectFromMap, 110 | }, 111 | }); 112 | 113 | const merged = deepExtend(target, source); 114 | 115 | expect(merged, "They should be the same instance.").to.equal(target); 116 | expect( 117 | merged, 118 | "Objects inheriting directly from Object should be deeply merged, other replaced." 119 | ).to.deep.equal({ 120 | objects: { 121 | objectLiteral: { 122 | p1: "T", 123 | p3: "S", 124 | }, 125 | objectFromNull: {}, 126 | objectFromObject: Object.create(Object), 127 | objectFromMap: new Map(), 128 | }, 129 | }); 130 | 131 | expect( 132 | merged.objects.objectLiteral, 133 | "Object literal should not be copied by reference." 134 | ).to.not.equal(source.objects.objectLiteral); 135 | expect( 136 | merged.objects.objectFromNull, 137 | "Object created from null should be copied by reference." 138 | ).to.equal(source.objects.objectFromNull); 139 | expect( 140 | merged.objects.objectFromObject, 141 | "Object created from null should be copied by reference." 142 | ).to.equal(source.objects.objectFromObject); 143 | expect( 144 | merged.objects.objectFromMap, 145 | "Object created from null should be copied by reference." 146 | ).to.equal(source.objects.objectFromMap); 147 | }); 148 | 149 | describe("inherited properties", function (): void { 150 | it("ignored by default", function (): void { 151 | const target = {}; 152 | const source = deepFreeze( 153 | Object.create( 154 | deepFreeze({ 155 | inherited: "S", 156 | }) 157 | ) 158 | ); 159 | 160 | const merged = deepExtend(target, source); 161 | 162 | expect(merged, "They should be the same instance.").to.equal(target); 163 | expect( 164 | merged, 165 | "Inherited properties shouldn’t be inherited by default." 166 | ).to.deep.equal({}); 167 | }); 168 | 169 | it("inherited if enabled", function (): void { 170 | const target = {}; 171 | const source = deepFreeze( 172 | Object.create( 173 | deepFreeze({ 174 | inherited: "S", 175 | }) 176 | ) 177 | ); 178 | 179 | const merged = deepExtend(target, source, true); 180 | 181 | expect(merged, "They should be the same instance.").to.equal(target); 182 | expect( 183 | merged, 184 | "Inherited properties should be inherited when enabled." 185 | ).to.deep.equal({ 186 | inherited: "S", 187 | }); 188 | }); 189 | }); 190 | 191 | describe("deletion", function (): void { 192 | it("disabled", function (): void { 193 | const target = { 194 | p1: "p1 T", 195 | p2: "p2 T", 196 | }; 197 | const source = deepFreeze({ 198 | p2: null, 199 | p3: null, 200 | }); 201 | 202 | const merged = deepExtend(target, source); 203 | 204 | expect(merged, "They should be the same instance.").to.equal(target); 205 | expect( 206 | merged, 207 | "No properties should be deleted unless enabled." 208 | ).to.deep.equal({ 209 | p1: "p1 T", 210 | p2: null, 211 | p3: null, 212 | }); 213 | }); 214 | 215 | it("enabled", function (): void { 216 | const target = { 217 | p1: "p1 T", 218 | p2: "p2 T", 219 | }; 220 | const source = deepFreeze({ 221 | p2: null, 222 | p3: null, 223 | }); 224 | 225 | const merged = deepExtend(target, source, false, true); 226 | 227 | expect(merged, "They should be the same instance.").to.equal(target); 228 | expect( 229 | merged, 230 | "Null properties from the source should delete matching properties in the target." 231 | ).to.deep.equal({ 232 | p1: "p1 T", 233 | p3: null, // TODO: This seems wrong. 234 | }); 235 | }); 236 | }); 237 | 238 | describe("edge cases", function (): void { 239 | it("constructor property", function (): void { 240 | const target = { 241 | object: { 242 | constructor: { 243 | p1: "T", 244 | }, 245 | }, 246 | }; 247 | const source = deepFreeze({ 248 | object: { 249 | constructor: { 250 | p3: "S", 251 | }, 252 | }, 253 | }); 254 | 255 | const merged = deepExtend(target, source); 256 | 257 | expect(merged, "They should be the same instance.").to.equal(target); 258 | expect( 259 | merged, 260 | "All the properties should be deeply merged." 261 | ).to.deep.equal({ 262 | object: { 263 | constructor: { 264 | p1: "T", 265 | p3: "S", 266 | }, 267 | }, 268 | }); 269 | }); 270 | }); 271 | }); 272 | -------------------------------------------------------------------------------- /test/deep-object-assign.errors.ts: -------------------------------------------------------------------------------- 1 | import { Assignable, DELETE, deepObjectAssign } from "../src"; 2 | 3 | interface TestObject { 4 | string?: string; 5 | number?: number; 6 | array?: number[]; 7 | object?: { 8 | string?: string; 9 | number?: number; 10 | array?: number[]; 11 | }; 12 | required: { 13 | demanded: string; 14 | }; 15 | } 16 | 17 | const testObject: TestObject = {} as any; 18 | 19 | let assignableTestObject: Assignable = {} as any; 20 | assignableTestObject = assignableTestObject ?? {}; 21 | 22 | // It shouldn't be possible to use anything but TestObject as the target. 23 | deepObjectAssign( 24 | // THROWS Argument of type 25 | {} 26 | ); 27 | 28 | // It shouldn't be possible to use Assignable as the target. 29 | deepObjectAssign( 30 | // THROWS Argument of type 31 | assignableTestObject 32 | ); 33 | 34 | deepObjectAssign( 35 | testObject, 36 | // It should be possible to assign the original object. 37 | testObject, 38 | // It should be possible to assign Assignable. 39 | assignableTestObject 40 | ); 41 | 42 | // It should be possible to replace optional values with DELETE. 43 | deepObjectAssign(testObject, { 44 | string: DELETE, 45 | number: DELETE, 46 | array: DELETE, 47 | object: { 48 | string: DELETE, 49 | number: DELETE, 50 | array: DELETE, 51 | }, 52 | }); 53 | 54 | // It shouldn't be possible to replace required values with DELETE. 55 | deepObjectAssign(testObject, { 56 | required: { 57 | // THROWS Type 'symbol' is not assignable to type 58 | demanded: DELETE, 59 | }, 60 | }); 61 | deepObjectAssign(testObject, { 62 | // THROWS Type 'symbol' has no properties in common with type '{ demanded?: string | undefined; }'. 63 | required: DELETE, 64 | }); 65 | 66 | // It should be possible to replace whole optional objects with DELETE. 67 | deepObjectAssign(testObject, { 68 | string: DELETE, 69 | number: DELETE, 70 | array: DELETE, 71 | object: DELETE, 72 | }); 73 | 74 | // It should be possible to omit properties. 75 | deepObjectAssign(testObject, { 76 | string: DELETE, 77 | object: { 78 | array: DELETE, 79 | }, 80 | }); 81 | deepObjectAssign(testObject, { 82 | array: DELETE, 83 | object: DELETE, 84 | }); 85 | deepObjectAssign(testObject, { 86 | array: DELETE, 87 | }); 88 | deepObjectAssign(testObject, {}); 89 | -------------------------------------------------------------------------------- /test/deep-object-assign.test.ts: -------------------------------------------------------------------------------- 1 | import { given, test } from "sazerac"; 2 | 3 | import { DELETE, deepObjectAssign } from "../src"; 4 | 5 | // Symbols don't currently work in the library, they're either completely 6 | // ignored or outright throw errors when tested. 7 | 8 | // const SYMBOL_KEY = Symbol("key"); 9 | // const SYMBOL_ORIGINAL = Symbol("original"); 10 | // const SYMBOL_NEW = Symbol("new"); 11 | 12 | test(deepObjectAssign, (): void => { 13 | given({}).expect({}); 14 | given({}, {}).expect({}); 15 | given({}, {}, {}).expect({}); 16 | 17 | given({ zero: 0, two: 0 }, { one: 1, two: 2 }).expect({ 18 | zero: 0, 19 | one: 1, 20 | two: 2, 21 | }); 22 | 23 | given({ zero: [0], two: [0, 0] }, { one: [1], two: [2] }).expect({ 24 | zero: [0], 25 | one: [1], 26 | two: [2], 27 | }); 28 | 29 | given( 30 | { zero: [0], two: [[0], 0] }, 31 | { one: [[1], { ONE: 1 }], two: [{ TWO: [2] }] } 32 | ).expect({ 33 | zero: [0], 34 | one: [[1], { ONE: 1 }], 35 | two: [{ TWO: [2] }], 36 | }); 37 | 38 | given( 39 | { zero: 0, two: 0 }, 40 | { one: 1, two: 1 }, 41 | { two: 2 }, 42 | { nested: { threeThenOne: 1 } } 43 | ).expect({ 44 | zero: 0, 45 | one: 1, 46 | two: 2, 47 | nested: { threeThenOne: 1 }, 48 | }); 49 | 50 | given( 51 | { zero: 0, two: 0 }, 52 | { one: 1, two: 1 }, 53 | { two: 2 }, 54 | { nested: { threeThenOne: 1 } } 55 | ).expect({ 56 | zero: 0, 57 | one: 1, 58 | two: 2, 59 | nested: { threeThenOne: 1 }, 60 | }); 61 | 62 | given( 63 | { 64 | bolean: true, 65 | id: 0.25, 66 | number: 37, 67 | string: "oops", 68 | // symbol: SYMBOL_ORIGINAL, 69 | }, 70 | { 71 | bolean: true, 72 | number: 42, 73 | string: "yay", 74 | // symbol: SYMBOL_NEW, 75 | } 76 | ).expect({ 77 | bolean: true, 78 | id: 0.25, 79 | number: 42, 80 | string: "yay", 81 | // symbol: SYMBOL_NEW, 82 | }); 83 | 84 | given( 85 | { 86 | bolean: false, 87 | id: 0.25, 88 | number: Number.NaN, 89 | string: "", 90 | // symbol: SYMBOL_ORIGINAL, 91 | }, 92 | { 93 | bolean: true, 94 | number: 42, 95 | string: "yay", 96 | // symbol: SYMBOL_NEW, 97 | } 98 | ).expect({ 99 | bolean: true, 100 | id: 0.25, 101 | number: 42, 102 | string: "yay", 103 | // symbol: SYMBOL_NEW, 104 | }); 105 | 106 | given( 107 | { 108 | bolean: true, 109 | id: 0.25, 110 | number: 4, 111 | string: "oops", 112 | // symbol: SYMBOL_ORIGINAL, 113 | }, 114 | { 115 | bolean: false, 116 | number: Number.NaN, 117 | string: "", 118 | // symbol: SYMBOL_NEW, 119 | } 120 | ).expect({ 121 | bolean: false, 122 | id: 0.25, 123 | number: Number.NaN, 124 | string: "", 125 | // symbol: SYMBOL_NEW, 126 | }); 127 | 128 | given( 129 | { 130 | bolean: true, 131 | id: 0.25, 132 | number: 4, 133 | string: "oops", 134 | // symbol: SYMBOL_ORIGINAL, 135 | }, 136 | { 137 | bolean: DELETE, 138 | number: DELETE, 139 | string: DELETE, 140 | symbol: DELETE, 141 | } 142 | ).expect({ 143 | id: 0.25, 144 | }); 145 | 146 | given( 147 | { 148 | id: 0.25, 149 | one: { 150 | two: { 151 | three: { 152 | four: "oops", 153 | }, 154 | }, 155 | }, 156 | }, 157 | { 158 | one: DELETE, 159 | } 160 | ).expect({ 161 | id: 0.25, 162 | }); 163 | 164 | given( 165 | { 166 | id: 0.25, 167 | one: { 168 | two: { 169 | three: { 170 | four: "oops", 171 | }, 172 | }, 173 | }, 174 | }, 175 | { 176 | one: { miss: DELETE }, 177 | } 178 | ).expect({ 179 | id: 0.25, 180 | one: { 181 | two: { 182 | three: { 183 | four: "oops", 184 | }, 185 | }, 186 | }, 187 | }); 188 | 189 | given( 190 | { 191 | id: 0.25, 192 | one: { 193 | two: { 194 | three: { 195 | four: "oops", 196 | }, 197 | }, 198 | }, 199 | }, 200 | { 201 | double: { miss: DELETE }, 202 | } 203 | ).expect({ 204 | id: 0.25, 205 | one: { 206 | two: { 207 | three: { 208 | four: "oops", 209 | }, 210 | }, 211 | }, 212 | double: {}, 213 | }); 214 | 215 | given( 216 | { 217 | id: 0.25, 218 | one: { 219 | two: { 220 | survivor: "yay", 221 | three: { 222 | four: "oops", 223 | }, 224 | }, 225 | }, 226 | }, 227 | { 228 | one: { two: { three: DELETE } }, 229 | } 230 | ).expect({ 231 | id: 0.25, 232 | one: { 233 | two: { 234 | survivor: "yay", 235 | }, 236 | }, 237 | }); 238 | 239 | given( 240 | { 241 | id: 0.25, 242 | // [SYMBOL_KEY]: 1, 243 | }, 244 | { 245 | // [SYMBOL_KEY]: 2, 246 | } 247 | ).expect({ 248 | id: 0.25, 249 | // [SYMBOL_KEY]: 2, 250 | }); 251 | 252 | given({}, { foo: "bar", deleteFoo: "foo" }).expect({ 253 | foo: "bar", 254 | deleteFoo: "foo", 255 | }); 256 | 257 | const date = new Date(0); 258 | const date2 = new Date(50000); 259 | const date3 = new Date(150000); 260 | 261 | // Merge single date into empty object 262 | given( 263 | {}, 264 | { 265 | start: date, 266 | } 267 | ).expect({ 268 | start: date, 269 | }); 270 | 271 | // Merge single date into non-empty object (no overlap) 272 | given( 273 | { start: date }, 274 | { 275 | end: date2, 276 | } 277 | ).expect({ 278 | start: date, 279 | end: date2, 280 | }); 281 | 282 | // Merge single date into non-empty object (overlap) 283 | given( 284 | { start: date, end: date2 }, 285 | { 286 | end: date3, 287 | } 288 | ).expect({ 289 | start: date, 290 | end: date3, 291 | }); 292 | }); 293 | -------------------------------------------------------------------------------- /test/for-each.test.ts: -------------------------------------------------------------------------------- 1 | import { assert, spy } from "sinon"; 2 | 3 | import { forEach } from "../src"; 4 | 5 | describe("forEach", function (): void { 6 | it("Array", function (): void { 7 | const forEachSpy = spy(); 8 | const array = [-1, 0, 1]; 9 | 10 | forEach(array, forEachSpy); 11 | 12 | assert.calledWithExactly(forEachSpy.firstCall, -1, 0, array); 13 | assert.calledWithExactly(forEachSpy.secondCall, 0, 1, array); 14 | assert.calledWithExactly(forEachSpy.thirdCall, 1, 2, array); 15 | assert.callCount(forEachSpy, 3); 16 | }); 17 | 18 | it("Object", function (): void { 19 | const forEachSpy = spy(); 20 | const objectProto = { 21 | ignore: "me", 22 | }; 23 | const object = Object.create(objectProto); 24 | object.a = -1; 25 | object.b = 0; 26 | object.c = 1; 27 | 28 | forEach(object, forEachSpy); 29 | 30 | assert.calledWithExactly(forEachSpy, -1, "a", object); 31 | assert.calledWithExactly(forEachSpy, 0, "b", object); 32 | assert.calledWithExactly(forEachSpy, 1, "c", object); 33 | assert.neverCalledWith(forEachSpy, "me", "ignore", object); 34 | assert.callCount(forEachSpy, 3); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/get-absolute.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { stub } from "sinon"; 3 | 4 | import { getAbsoluteLeft, getAbsoluteRight, getAbsoluteTop } from "../src"; 5 | 6 | describe("getAbsolute*", function (): void { 7 | const elem = { getBoundingClientRect: stub() }; 8 | elem.getBoundingClientRect.returns({ 9 | top: 1, 10 | right: 2, 11 | bottom: 3, 12 | left: 4, 13 | }); 14 | 15 | it("getAbsoluteTop", function (): void { 16 | expect(getAbsoluteTop(elem as any)).to.equal(1); 17 | }); 18 | 19 | it("getAbsoluteRight", function (): void { 20 | expect(getAbsoluteRight(elem as any)).to.equal(2); 21 | }); 22 | 23 | it("getAbsoluteLeft", function (): void { 24 | expect(getAbsoluteLeft(elem as any)).to.equal(4); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/helpers/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Prevents supplied object from being modified. 3 | * 4 | * @remarks 5 | * Has no problem with cycles. 6 | * Can receive frozen or parially frozen objects. 7 | * @typeParam T - The type of the object. 8 | * @param object - The object to be recursively frozen. 9 | * @param freeze - Function that does the freezing. Object.seal or anything with the same API can be used instead. 10 | * @returns The frozen object (the same instance as object param). 11 | */ 12 | export function deepFreeze( 13 | object: T, 14 | freeze: (object: T) => T = (object: T): T => Object.freeze(object) 15 | ): T { 16 | const alreadyFrozen = new Set(); 17 | 18 | /** 19 | * Recursivelly freezes objects using alreadyFrozen to prevent infinite cycles. 20 | * 21 | * @param object - The object to be recursively frozen. 22 | * @returns The frozen object (the same instance as object param). 23 | */ 24 | function recursivelyFreeze(object: any): any { 25 | // Prevent double freezing (could lead to stack overflow due to an infinite cycle) 26 | // Object.isFrozen is not used here because frozen objects can have unfrozen objects in their properties. 27 | if (alreadyFrozen.has(object)) { 28 | return object; 29 | } 30 | alreadyFrozen.add(object); 31 | 32 | // Retrieve the property names defined on object 33 | const names = Object.getOwnPropertyNames(object); 34 | 35 | // Freeze properties before freezing the object 36 | for (const name of names) { 37 | const prop = object[name]; 38 | if (prop && typeof prop === "object") { 39 | recursivelyFreeze(prop); 40 | } else { 41 | freeze(prop); 42 | } 43 | } 44 | 45 | return freeze(object); 46 | } 47 | 48 | return recursivelyFreeze(object); 49 | } 50 | -------------------------------------------------------------------------------- /test/hex-to-rgb.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { hexToRGB, RGB } from "../src"; 4 | 5 | describe("hexToRGB", function (): void { 6 | const valid = [ 7 | { color: "#000000", expected: { r: 0x00, g: 0x00, b: 0x00 } }, 8 | { color: "#0acdc0", expected: { r: 0x0a, g: 0xcd, b: 0xc0 } }, 9 | { color: "#AC00DC", expected: { r: 0xac, g: 0x00, b: 0xdc } }, 10 | { color: "#09afAF", expected: { r: 0x09, g: 0xaf, b: 0xaf } }, 11 | { color: "#000", expected: { r: 0x00, g: 0x00, b: 0x00 } }, 12 | { color: "#0ac", expected: { r: 0x00, g: 0xaa, b: 0xcc } }, 13 | { color: "#0DC", expected: { r: 0x00, g: 0xdd, b: 0xcc } }, 14 | { color: "#09a", expected: { r: 0x00, g: 0x99, b: 0xaa } }, 15 | { color: "#fAF", expected: { r: 0xff, g: 0xaa, b: 0xff } }, 16 | ]; 17 | valid.push( 18 | // without # 19 | ...valid.map(({ color, expected }): { color: string; expected: RGB } => ({ 20 | color: color.slice(1), 21 | expected, 22 | })) 23 | ); 24 | 25 | const invalid = [ 26 | // 5 or 2 digits 27 | ...valid.map(({ color }): string => color.slice(0, -1)), 28 | // 4 or 1 digit 29 | ...valid.map(({ color }): string => color.slice(0, -2)), 30 | // 7 or 4 digits 31 | ...valid.map(({ color }): string => color + "0"), 32 | // 8 or 5 digits 33 | ...valid.map(({ color }): string => color + "Fa"), 34 | " #000000", 35 | " ", 36 | "##abc", 37 | "#000 ", 38 | "#ABC is a color", 39 | "#abc-ef", 40 | "#Ř0AABB", 41 | "", 42 | "0", 43 | "false", 44 | "garbage", 45 | "orange", 46 | "the color is #00AAAA", 47 | "true", 48 | ]; 49 | 50 | describe("Valid", function (): void { 51 | valid.forEach(({ color, expected }): void => { 52 | it(color, function (): void { 53 | expect(hexToRGB(color)).to.be.deep.equal(expected); 54 | }); 55 | }); 56 | }); 57 | 58 | describe("Invalid", function (): void { 59 | invalid.forEach((color): void => { 60 | it(color, function (): void { 61 | expect(hexToRGB(color)).to.be.null; 62 | }); 63 | }); 64 | }); 65 | }); 66 | -------------------------------------------------------------------------------- /test/hsv-to-rgb.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { HSVToRGB, RGB } from "../src"; 4 | 5 | describe("HSVToRGB", function (): void { 6 | const valid: { args: [number, number, number]; expected: RGB }[] = [ 7 | { args: [0, 0, 0], expected: { r: 0, g: 0, b: 0 } }, 8 | { args: [0.25, 0, 0], expected: { r: 0, g: 0, b: 0 } }, 9 | { args: [0.5, 0, 0], expected: { r: 0, g: 0, b: 0 } }, 10 | { args: [0.75, 0, 0], expected: { r: 0, g: 0, b: 0 } }, 11 | { args: [1, 0, 0], expected: { r: 0, g: 0, b: 0 } }, 12 | 13 | { args: [0.2, 0.3, 1], expected: { r: 239, g: 255, b: 178 } }, 14 | { args: [0.4, 0.3, 0.7], expected: { r: 124, g: 178, b: 146 } }, 15 | { args: [0.6, 0.8, 0.3], expected: { r: 15, g: 39, b: 76 } }, 16 | { args: [0.8, 1, 0.3], expected: { r: 61, g: 0, b: 76 } }, 17 | { args: [0.95, 0.5, 0.5], expected: { r: 127, g: 63, b: 82 } }, 18 | 19 | { args: [0, 1, 1], expected: { r: 255, g: 0, b: 0 } }, 20 | { args: [0.25, 1, 1], expected: { r: 127, g: 255, b: 0 } }, 21 | { args: [0.5, 1, 1], expected: { r: 0, g: 255, b: 255 } }, 22 | { args: [0.75, 1, 1], expected: { r: 127, g: 0, b: 255 } }, 23 | { args: [1, 1, 1], expected: { r: 255, g: 0, b: 0 } }, 24 | ]; 25 | 26 | describe("Valid", function (): void { 27 | valid.forEach(({ args, expected }): void => { 28 | it(JSON.stringify(args), function (): void { 29 | expect(HSVToRGB(...args)).to.be.deep.equal(expected); 30 | }); 31 | }); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /test/insert-sort.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { insertSort } from "../src"; 4 | 5 | describe("insertSort", function (): void { 6 | it("Sorted data should stay in the same order", function (): void { 7 | const getData = (): number[] => [ 8 | Number.NEGATIVE_INFINITY, 9 | Number.MIN_SAFE_INTEGER, 10 | -7, 11 | -1, 12 | 0, 13 | Number.MIN_VALUE, 14 | 0.001, 15 | 1, 16 | Math.PI, 17 | 10, 18 | 20, 19 | 23.4, 20 | 156, 21 | 1000, 22 | 2000, 23 | Number.MAX_SAFE_INTEGER, 24 | Number.MAX_VALUE, 25 | Number.POSITIVE_INFINITY, 26 | ]; 27 | 28 | expect(insertSort(getData(), (a, b): number => a - b)).to.deep.equal( 29 | getData() 30 | ); 31 | }); 32 | 33 | it("Same values should never swap places", function (): void { 34 | const getData = (): { index?: number; value: number }[] => [ 35 | { value: 11 }, 36 | { index: 6, value: 7 }, 37 | { index: 7, value: 7 }, 38 | { value: -23 }, 39 | { value: -28 }, 40 | { value: 21 }, 41 | { value: -16 }, 42 | { value: 27 }, 43 | { index: 8, value: 7 }, 44 | { value: -27 }, 45 | { index: 9, value: 7 }, 46 | { value: -7 }, 47 | { value: 48 }, 48 | { index: 10, value: 7 }, 49 | { value: -77 }, 50 | { value: 22 }, 51 | ]; 52 | 53 | const sorted = insertSort(getData(), (a, b): number => a.value - b.value); 54 | sorted.forEach(({ index }, arrayIndex): void => { 55 | if (index != null) { 56 | expect(index).to.equal(arrayIndex); 57 | } 58 | }); 59 | }); 60 | }); 61 | -------------------------------------------------------------------------------- /test/is-valid-hex.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { isValidHex } from "../src"; 4 | 5 | describe("isValidHex", function (): void { 6 | const valid = [ 7 | "#000000", 8 | "#0acdc0", 9 | "#AC00DC", 10 | "#09afAF", 11 | "#000", 12 | "#0ac", 13 | "#0DC", 14 | "#09a", 15 | "#fAF", 16 | ]; 17 | const invalid = [ 18 | // without # 19 | ...valid.map((color): string => color.slice(1)), 20 | // 5 or 2 digits 21 | ...valid.map((color): string => color.slice(0, -1)), 22 | // 4 or 1 digit 23 | ...valid.map((color): string => color.slice(0, -2)), 24 | // 7 or 4 digits 25 | ...valid.map((color): string => color + "0"), 26 | // 8 or 5 digits 27 | ...valid.map((color): string => color + "Fa"), 28 | " #000000", 29 | " ", 30 | "##abc", 31 | "#000 ", 32 | "#ABC is a color", 33 | "#abc-ef", 34 | "#Ř0AABB", 35 | "", 36 | "0", 37 | "false", 38 | "garbage", 39 | "orange", 40 | "the color is #00AAAA", 41 | "true", 42 | ]; 43 | 44 | describe("Valid", function (): void { 45 | valid.forEach((color): void => { 46 | it(color, function (): void { 47 | expect(isValidHex(color)).to.be.true; 48 | }); 49 | }); 50 | }); 51 | 52 | describe("Invalid", function (): void { 53 | invalid.forEach((color): void => { 54 | it(color, function (): void { 55 | expect(isValidHex(color)).to.be.false; 56 | }); 57 | }); 58 | }); 59 | }); 60 | -------------------------------------------------------------------------------- /test/is-valid-rgb.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { isValidRGB } from "../src"; 4 | 5 | describe("isValidRGB", function (): void { 6 | const valid = [ 7 | "RGB(7, 200, 8)", 8 | "RGb(255,255,255)", 9 | "Rgb(0,12,123)", 10 | "rGB(44, 7, 220)", 11 | "rGb(210, 50,220)", 12 | "rgB(210,50, 220)", 13 | "rgb( 72 , 11 , 123 )", 14 | "rgb(0,0,0)", 15 | ]; 16 | const invalid = [ 17 | " ", 18 | " Rgb(0,12,123) ", 19 | " rGb(210, 50,220)", 20 | "#000000", 21 | "#abc", 22 | "", 23 | "(0,12,123)", 24 | "0", 25 | "0,12,123)", 26 | "5,7,9", 27 | "RGB(-1, 0, 0)", 28 | "RGB(0, -1, 0)", 29 | "RGB(0, 0, -1)", 30 | "RGB(255, 255, 256)", 31 | "RGB(255, 256, 255)", 32 | "RGB(256, 255, 255)", 33 | "RGBA(123, 147, 95, 1)", 34 | "false", 35 | "garbage", 36 | "hi rgb(0,12,123)", 37 | "orange", 38 | "rGb(210, 50,220) ", 39 | "rgb 7, 7, 7", 40 | "rgb(0, 12, 123) :-)", 41 | "rgb(0,12,123", 42 | "rgb(4 4, 7, 220)", 43 | "rgb(44, 7, 2 2 0)", 44 | "rgba(7,8,9,0.3)", 45 | "the color is #00AAAA", 46 | "true", 47 | ]; 48 | 49 | describe("Valid", function (): void { 50 | valid.forEach((color): void => { 51 | it(color, function (): void { 52 | expect(isValidRGB(color)).to.be.true; 53 | }); 54 | }); 55 | }); 56 | 57 | describe("Invalid", function (): void { 58 | invalid.forEach((color): void => { 59 | it(color, function (): void { 60 | expect(isValidRGB(color)).to.be.false; 61 | }); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /test/is-valid-rgba.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { isValidRGBA } from "../src"; 4 | 5 | describe("isValidRGBA", function (): void { 6 | const valid = [ 7 | "RGBA(7, 200, 8, .7)", 8 | "RGBA(7, 200, 8, 0.7)", 9 | "RGba(255,255,255,1)", 10 | "Rgba(0,12,123,0.321)", 11 | "rGBa(44, 7, 220,0.92)", 12 | "rGba(210, 50,220, 0.42)", 13 | "rgBa(210,50, 220,0.37)", 14 | "rgbA( 72 , 11 , 123 , 0.21 )", 15 | "rgba(0,0,0,0)", 16 | ]; 17 | const invalid = [ 18 | " ", 19 | " RGBA(0, 12, 123, 0.3) ", 20 | " RGBA(210,50,220,0)", 21 | "#000000", 22 | "#abc", 23 | "", 24 | "(0,12,123)", 25 | "0", 26 | "0,12,123)", 27 | "5,7,9", 28 | "RGBA(210, 50, 220, 0.77) ", 29 | "false", 30 | "garbage", 31 | "hi rgb(0,12,123)", 32 | "orange", 33 | "rgb 7, 7, 7", 34 | "rgb(0, 12, 123)", 35 | "rgb(0,12,123, 0.2)", 36 | "rgba(0, 0, -1, 0)", 37 | "rgba(0, 0, 0, -1)", 38 | "rgba(0, 0, 0, 1.1)", 39 | "rgba(0, 0, 0, 2)", 40 | "rgba(0, 12, 123, 0.7) :-)", 41 | "rgba(0, 300, 0, 0)", 42 | "rgba(0,1 2,0,0)", 43 | "rgba(0,12,123,0.3", 44 | "rgba(256, 0, 0, 0)", 45 | "rgba(7, 8, 9)", 46 | "rgba(7,8,9)", 47 | "the color is #00AAAA", 48 | "true", 49 | ]; 50 | 51 | describe("Valid", function (): void { 52 | valid.forEach((color): void => { 53 | it(color, function (): void { 54 | expect(isValidRGBA(color)).to.be.true; 55 | }); 56 | }); 57 | }); 58 | 59 | describe("Invalid", function (): void { 60 | invalid.forEach((color): void => { 61 | it(color, function (): void { 62 | expect(isValidRGBA(color)).to.be.false; 63 | }); 64 | }); 65 | }); 66 | }); 67 | -------------------------------------------------------------------------------- /test/package.test.ts: -------------------------------------------------------------------------------- 1 | import snapshot from "snap-shot-it"; 2 | import { inspectNpmPack } from "vis-dev-utils"; 3 | 4 | describe("Package", function (): void { 5 | it("Exported files", function (): void { 6 | this.timeout("5m"); 7 | snapshot(inspectNpmPack()); 8 | }); 9 | }); 10 | -------------------------------------------------------------------------------- /test/parse-color.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { parseColor, ColorObject, FullColorObject } from "../src"; 4 | 5 | describe("parseColor", function (): void { 6 | describe("strings", function (): void { 7 | const inputs: { color: string; expected: FullColorObject }[] = [ 8 | { 9 | color: "Hi, I’m a color :-).", 10 | expected: { 11 | background: "Hi, I’m a color :-).", 12 | border: "Hi, I’m a color :-).", 13 | hover: { 14 | background: "Hi, I’m a color :-).", 15 | border: "Hi, I’m a color :-).", 16 | }, 17 | highlight: { 18 | background: "Hi, I’m a color :-).", 19 | border: "Hi, I’m a color :-).", 20 | }, 21 | }, 22 | }, 23 | { 24 | color: "rgb(0, 119, 238)", 25 | expected: { 26 | background: "#0077ee", 27 | border: "#005fbe", 28 | hover: { 29 | background: "#3091f2", 30 | border: "#005fbe", 31 | }, 32 | highlight: { 33 | background: "#3091f2", 34 | border: "#005fbe", 35 | }, 36 | }, 37 | }, 38 | { 39 | color: "#0077EE", 40 | expected: { 41 | background: "#0077EE", 42 | border: "#005fbe", 43 | hover: { 44 | background: "#3091f2", 45 | border: "#005fbe", 46 | }, 47 | highlight: { 48 | background: "#3091f2", 49 | border: "#005fbe", 50 | }, 51 | }, 52 | }, 53 | ]; 54 | 55 | inputs.forEach(({ color, expected }): void => { 56 | it(color, function (): void { 57 | expect(parseColor(color)).to.be.deep.equal(expected); 58 | }); 59 | }); 60 | }); 61 | 62 | describe("color objects", function (): void { 63 | const inputs: { 64 | name: string; 65 | color: ColorObject; 66 | defaultColor?: FullColorObject; 67 | expected: ColorObject; 68 | }[] = [ 69 | { 70 | name: "empty object without default color", 71 | color: {}, 72 | expected: { 73 | background: undefined, 74 | border: undefined, 75 | hover: { 76 | background: undefined, 77 | border: undefined, 78 | }, 79 | highlight: { 80 | background: undefined, 81 | border: undefined, 82 | }, 83 | }, 84 | }, 85 | { 86 | name: "empty object with default color", 87 | color: {}, 88 | defaultColor: { 89 | border: "rgb(0, 1, 2)", 90 | background: "rgb(3, 4, 5)", 91 | hover: { 92 | border: "rgb(6, 7, 8)", 93 | background: "rgb(9, 10, 11)", 94 | }, 95 | highlight: { 96 | border: "rgb(12, 13, 14)", 97 | background: "rgb(15, 16, 17)", 98 | }, 99 | }, 100 | expected: { 101 | border: "rgb(0, 1, 2)", 102 | background: "rgb(3, 4, 5)", 103 | hover: { 104 | border: "rgb(6, 7, 8)", 105 | background: "rgb(9, 10, 11)", 106 | }, 107 | highlight: { 108 | border: "rgb(12, 13, 14)", 109 | background: "rgb(15, 16, 17)", 110 | }, 111 | }, 112 | }, 113 | { 114 | name: "partial without default color", 115 | color: { 116 | border: "#0077EE", 117 | hover: { 118 | background: "#123456", 119 | }, 120 | highlight: { 121 | border: "#ABCDEF", 122 | }, 123 | }, 124 | expected: { 125 | background: undefined, 126 | border: "#0077EE", 127 | hover: { 128 | background: "#123456", 129 | border: undefined, 130 | }, 131 | highlight: { 132 | background: undefined, 133 | border: "#ABCDEF", 134 | }, 135 | }, 136 | }, 137 | { 138 | name: "full without default color", 139 | color: { 140 | border: "rgb(0, 1, 2)", 141 | background: "rgb(3, 4, 5)", 142 | hover: { 143 | border: "rgb(6, 7, 8)", 144 | background: "rgb(9, 10, 11)", 145 | }, 146 | highlight: { 147 | border: "rgb(12, 13, 14)", 148 | background: "rgb(15, 16, 17)", 149 | }, 150 | }, 151 | expected: { 152 | border: "rgb(0, 1, 2)", 153 | background: "rgb(3, 4, 5)", 154 | hover: { 155 | border: "rgb(6, 7, 8)", 156 | background: "rgb(9, 10, 11)", 157 | }, 158 | highlight: { 159 | border: "rgb(12, 13, 14)", 160 | background: "rgb(15, 16, 17)", 161 | }, 162 | }, 163 | }, 164 | { 165 | name: "partial with default color", 166 | color: { 167 | border: "#023456", 168 | hover: { 169 | background: "#034567", 170 | }, 171 | highlight: { 172 | border: "#06789A", 173 | }, 174 | }, 175 | defaultColor: { 176 | background: "#112345", 177 | border: "#123456", 178 | hover: { 179 | background: "#134567", 180 | border: "#145678", 181 | }, 182 | highlight: { 183 | background: "#156789", 184 | border: "#16789A", 185 | }, 186 | }, 187 | expected: { 188 | background: "#112345", 189 | border: "#023456", 190 | hover: { 191 | background: "#034567", 192 | border: "#145678", 193 | }, 194 | highlight: { 195 | background: "#156789", 196 | border: "#06789A", 197 | }, 198 | }, 199 | }, 200 | { 201 | name: "strings without default color", 202 | color: { 203 | background: "#012345", 204 | border: "#023456", 205 | hover: "#034567", 206 | highlight: "#06789A", 207 | }, 208 | expected: { 209 | background: "#012345", 210 | border: "#023456", 211 | hover: { 212 | background: "#034567", 213 | border: "#034567", 214 | }, 215 | highlight: { 216 | background: "#06789A", 217 | border: "#06789A", 218 | }, 219 | }, 220 | }, 221 | { 222 | name: "strings with default color", 223 | color: { 224 | background: "#012345", 225 | border: "#023456", 226 | hover: "#034567", 227 | highlight: "#045678", 228 | }, 229 | defaultColor: { 230 | background: "#112345", 231 | border: "#123456", 232 | hover: { 233 | background: "#134567", 234 | border: "#145678", 235 | }, 236 | highlight: { 237 | background: "#156789", 238 | border: "#16789A", 239 | }, 240 | }, 241 | expected: { 242 | background: "#012345", 243 | border: "#023456", 244 | hover: { 245 | background: "#034567", 246 | border: "#034567", 247 | }, 248 | highlight: { 249 | background: "#045678", 250 | border: "#045678", 251 | }, 252 | }, 253 | }, 254 | { 255 | name: "default color only", 256 | color: {}, 257 | defaultColor: { 258 | background: "#112345", 259 | border: "#123456", 260 | hover: { 261 | background: "#134567", 262 | border: "#145678", 263 | }, 264 | highlight: { 265 | background: "#156789", 266 | border: "#16789A", 267 | }, 268 | }, 269 | expected: { 270 | background: "#112345", 271 | border: "#123456", 272 | hover: { 273 | background: "#134567", 274 | border: "#145678", 275 | }, 276 | highlight: { 277 | background: "#156789", 278 | border: "#16789A", 279 | }, 280 | }, 281 | }, 282 | ]; 283 | 284 | inputs.forEach(({ name, color, defaultColor, expected }): void => { 285 | it(name, function (): void { 286 | expect( 287 | parseColor(color, defaultColor as FullColorObject) 288 | ).to.be.deep.equal(expected); 289 | }); 290 | }); 291 | }); 292 | }); 293 | -------------------------------------------------------------------------------- /test/random/alea.test.ts: -------------------------------------------------------------------------------- 1 | import snapshot from "snap-shot-it"; 2 | 3 | import { Alea, RNG } from "../../src/random"; 4 | 5 | describe("Alea", function (): void { 6 | this.timeout(60000); 7 | 8 | const count = 100; 9 | 10 | for (const { name, get } of [ 11 | { name: "rng()", get: (rng: RNG): number => rng() }, 12 | { name: "rng.fract53()", get: (rng: RNG): number => rng.fract53() }, 13 | { name: "rng.uint32()", get: (rng: RNG): number => rng.uint32() }, 14 | ]) { 15 | describe(name, function (): void { 16 | for (const seed of [ 17 | [ 18 | "I'm an alligator", 19 | " I'm a mama-papa coming for you", 20 | " I'm the space invader", 21 | " I'll be a rock 'n' rollin' bitch for you.", 22 | ], 23 | +new Date("2020-01-01"), 24 | false, 25 | true, 26 | Math.PI, 27 | BigInt("4235467986087964853726437"), 28 | -0.00432, 29 | -423, 30 | 0.1244942, 31 | 77, 32 | 0, 33 | "We can be zeros, just for one day", 34 | ]) { 35 | it(`Seed: ${seed.toString()}`, function (): void { 36 | const alea = Alea(seed); 37 | 38 | const values = Array(count); 39 | for (let i = 0; i < count; ++i) { 40 | values[i] = get(alea); 41 | } 42 | 43 | // Note: All the code above could be replaced by two args for data 44 | // driven snapshots however the overhead of that is insane. The tests 45 | // would go from milliseconds to seconds. 46 | snapshot(values); 47 | }); 48 | } 49 | }); 50 | } 51 | }); 52 | -------------------------------------------------------------------------------- /test/remove-class-name.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { removeClassName } from "../src"; 4 | 5 | describe("removeClassName", function (): void { 6 | const inputs: { input: string; classes: string; expected: string }[] = [ 7 | { input: "a b c", classes: "a c", expected: "b" }, 8 | { input: "a b c", classes: "b", expected: "a c" }, 9 | { input: "a b c", classes: "d", expected: "a b c" }, 10 | { input: "class-1 class-2", classes: "class-2", expected: "class-1" }, 11 | ]; 12 | 13 | inputs.forEach(({ input, classes, expected }): void => { 14 | it(`${input} - ${classes} = ${expected}`, function (): void { 15 | const elem = { className: input }; 16 | 17 | removeClassName(elem as any, classes); 18 | 19 | expect(elem.className).to.equal(expected); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/remove-css-text.test.ts: -------------------------------------------------------------------------------- 1 | import jsdom_global from "jsdom-global"; 2 | import { expect } from "chai"; 3 | 4 | import { removeCssText } from "../src"; 5 | 6 | describe("removeCssText", function (): void { 7 | beforeEach(function () { 8 | this.jsdom_global = jsdom_global(); 9 | }); 10 | 11 | afterEach(function () { 12 | this.jsdom_global(); 13 | }); 14 | 15 | it("Remove color and background URL from nothing", function (): void { 16 | const element = document.createElement("div"); 17 | element.style.cssText = ""; 18 | 19 | removeCssText( 20 | element, 21 | "color: blue; background: url(http://www.example.com:8080/b.jpg);" 22 | ); 23 | 24 | expect(element.style.cssText).to.equal(""); 25 | }); 26 | 27 | it("Remove nothing from color, margin and background URL", function (): void { 28 | const element = document.createElement("div"); 29 | element.style.cssText = 30 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 31 | 32 | removeCssText(element, ""); 33 | 34 | expect(element.style.cssText).to.equal( 35 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);" 36 | ); 37 | }); 38 | 39 | it("Remove padding from color, margin and background URL", function (): void { 40 | const element = document.createElement("div"); 41 | element.style.cssText = 42 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 43 | 44 | removeCssText(element, "padding: 4ex;"); 45 | 46 | expect(element.style.cssText).to.equal( 47 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);" 48 | ); 49 | }); 50 | 51 | it("Remove color and background URL from color, margin and background URL", function (): void { 52 | const element = document.createElement("div"); 53 | element.style.cssText = 54 | "color: red; margin: 1em; background: url(http://www.example.com:8080/a.jpg);"; 55 | 56 | removeCssText( 57 | element, 58 | "color: blue; background: url(http://www.example.com:8080/b.jpg);" 59 | ); 60 | 61 | expect(element.style.cssText).to.equal("margin: 1em;"); 62 | }); 63 | }); 64 | -------------------------------------------------------------------------------- /test/rgb-to-hex.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { RGBToHex } from "../src"; 4 | 5 | describe("RGBToHex", function (): void { 6 | const valid: { args: [number, number, number]; expected: string }[] = [ 7 | { args: [0x00, 0x00, 0x00], expected: "#000000" }, 8 | { args: [0x00, 0x99, 0xaa], expected: "#0099aa" }, 9 | { args: [0x00, 0xaa, 0xcc], expected: "#00aacc" }, 10 | { args: [0x00, 0xdd, 0xcc], expected: "#00ddcc" }, 11 | { args: [0x09, 0xaf, 0xaf], expected: "#09afaf" }, 12 | { args: [0x0a, 0xcd, 0xc0], expected: "#0acdc0" }, 13 | { args: [0xac, 0x00, 0xdc], expected: "#ac00dc" }, 14 | { args: [0xff, 0xaa, 0xff], expected: "#ffaaff" }, 15 | { args: [0xff, 0xff, 0xff], expected: "#ffffff" }, 16 | ]; 17 | 18 | describe("Valid", function (): void { 19 | valid.forEach(({ args, expected }): void => { 20 | it(JSON.stringify(args), function (): void { 21 | expect(RGBToHex(...args)).to.be.deep.equal(expected); 22 | }); 23 | }); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/selective-deep-extend.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { selectiveDeepExtend } from "../src"; 4 | 5 | describe("selectiveDeepExtend", function (): void { 6 | describe("copy 1, overwrite 1, ignore 1", function (): void { 7 | it("shallow → shallow", function (): void { 8 | const target = { ignored: "target", merged: "target" }; 9 | const source = Object.freeze({ 10 | ignored: "source", 11 | merged: "source", 12 | copied: "source", 13 | }); 14 | const copied = selectiveDeepExtend(["merged", "copied"], target, source); 15 | 16 | expect(copied, "They should be the same instance.").to.equal(target); 17 | expect( 18 | copied, 19 | "The selected properties of the objects should be deeply merged" 20 | ).to.deep.equal({ 21 | ignored: "target", 22 | merged: "source", 23 | copied: "source", 24 | }); 25 | }); 26 | 27 | it("deep → deep", function (): void { 28 | const target = { 29 | ignored: { nested: { prop: "target" }, additional: "target" }, 30 | merged: { nested: { prop: "target", additional: "target" } }, 31 | }; 32 | const source = Object.freeze({ 33 | ignored: { nested: { prop: "source", another: "source" } }, 34 | merged: { nested: { prop: "source" }, another: "source" }, 35 | copied: { nested: { prop: "source", another: "source" } }, 36 | }); 37 | const copied = selectiveDeepExtend(["merged", "copied"], target, source); 38 | 39 | expect(copied, "They should be the same instance.").to.equal(target); 40 | expect( 41 | copied, 42 | "The selected properties of the objects should be deeply merged" 43 | ).to.deep.equal({ 44 | ignored: { nested: { prop: "target" }, additional: "target" }, 45 | merged: { 46 | nested: { prop: "source", additional: "target" }, 47 | another: "source", 48 | }, 49 | copied: { nested: { prop: "source", another: "source" } }, 50 | }); 51 | }); 52 | 53 | it("primitive → deep", function (): void { 54 | const target = { 55 | ignored: { nested: { prop: "target" }, additional: "target" }, 56 | merged: { nested: { prop: "target", additional: "target" } }, 57 | }; 58 | const source = Object.freeze({ 59 | ignored: "source", 60 | merged: "source", 61 | copied: "source", 62 | }); 63 | const copied = selectiveDeepExtend(["merged", "copied"], target, source); 64 | 65 | expect(copied, "They should be the same instance.").to.equal(target); 66 | expect( 67 | copied, 68 | "The selected properties of the objects should be deeply merged" 69 | ).to.deep.equal({ 70 | ignored: { nested: { prop: "target" }, additional: "target" }, 71 | merged: "source", 72 | copied: "source", 73 | }); 74 | }); 75 | 76 | it("deep → primitive", function (): void { 77 | const target = { 78 | ignored: "target", 79 | merged: "target", 80 | }; 81 | const source = Object.freeze({ 82 | ignored: { nested: { prop: "source", another: "source" } }, 83 | merged: { nested: { prop: "source" }, another: "source" }, 84 | copied: { nested: { prop: "source", another: "source" } }, 85 | }); 86 | const copied = selectiveDeepExtend(["merged", "copied"], target, source); 87 | 88 | expect(copied, "They should be the same instance.").to.equal(target); 89 | expect( 90 | copied, 91 | "The selected properties of the objects should be deeply merged" 92 | ).to.deep.equal({ 93 | ignored: "target", 94 | merged: { nested: { prop: "source" }, another: "source" }, 95 | copied: { nested: { prop: "source", another: "source" } }, 96 | }); 97 | }); 98 | }); 99 | 100 | describe("arrays", function (): void { 101 | it("array source", function (): void { 102 | expect((): void => { 103 | selectiveDeepExtend(["prop"], {}, []); 104 | }).to.throw(); 105 | }); 106 | 107 | it("array → array", function (): void { 108 | const target = { 109 | ignored: ["target", "target"], 110 | merged: ["target", "target"], 111 | }; 112 | const source = Object.freeze({ 113 | ignored: ["source"], 114 | merged: ["source"], 115 | copied: ["source"], 116 | }); 117 | 118 | expect((): void => { 119 | selectiveDeepExtend(["merged", "copied"], target, source); 120 | }).to.throw(); 121 | }); 122 | }); 123 | }); 124 | -------------------------------------------------------------------------------- /test/selective-extend.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { selectiveExtend } from "../src"; 4 | 5 | describe("selectiveExtend", function (): void { 6 | it("non-array property names", function (): void { 7 | expect((): void => { 8 | selectiveExtend("prop" as any, {}, {}); 9 | }).to.throw(); 10 | }); 11 | 12 | it("copy 1 ignore 1", function (): void { 13 | const target = { hi: ":-)" }; 14 | const source = Object.freeze({ hi: ":-( 1", bye: ":-) 1" }); 15 | const copied = selectiveExtend(["bye"], target, source); 16 | 17 | expect(copied, "They should be the same instance.").to.equal(target); 18 | expect( 19 | copied, 20 | "The selected properties should be copied by reference" 21 | ).to.deep.equal({ 22 | hi: ":-)", 23 | bye: ":-) 1", 24 | }); 25 | }); 26 | 27 | it("2 sources, copy 1 ignore 1", function (): void { 28 | const target = { hi: ":-)" }; 29 | const source1 = Object.freeze({ hi: ":-( 1", bye: ":-) 1" }); 30 | const source2 = Object.freeze({ hi: ":-( 2", bye: ":-) 2" }); 31 | const copied = selectiveExtend(["bye"], target, source1, source2); 32 | 33 | expect(copied, "They should be the same instance.").to.equal(target); 34 | expect( 35 | copied, 36 | "The selected properties should be copied by reference" 37 | ).to.deep.equal({ 38 | hi: ":-)", 39 | bye: ":-) 2", 40 | }); 41 | }); 42 | 43 | it("3 sources, copy 2 ignore 1", function (): void { 44 | const target = { hi: ":-)" }; 45 | const source1 = Object.freeze({ hi: ":-( 1", bye: ":-) 1" }); 46 | const source2 = Object.freeze({ 47 | hi: ":-( 2", 48 | bye: ":-) 2", 49 | hello: ":-) 2", 50 | }); 51 | const source3 = Object.freeze({ hi: ":-( 3", bye: ":-) 3" }); 52 | const copied = selectiveExtend( 53 | ["bye", "hello"], 54 | target, 55 | source1, 56 | source2, 57 | source3 58 | ); 59 | 60 | expect(copied, "They should be the same instance.").to.equal(target); 61 | expect( 62 | copied, 63 | "The selected properties should be copied by reference" 64 | ).to.deep.equal({ 65 | hi: ":-)", 66 | hello: ":-) 2", 67 | bye: ":-) 3", 68 | }); 69 | }); 70 | }); 71 | -------------------------------------------------------------------------------- /test/throttle.test.ts: -------------------------------------------------------------------------------- 1 | import { assert, spy } from "sinon"; 2 | 3 | import { throttle } from "../src"; 4 | 5 | describe("throttle", function (): void { 6 | const queue: ((str: string) => void)[] = []; 7 | const fire = (): void => { 8 | queue.splice(0).forEach((fn): void => { 9 | fn("This should never be seen in the throttled callback."); 10 | }); 11 | }; 12 | 13 | beforeEach((): void => { 14 | (global as any).requestAnimationFrame = queue.push.bind(queue); 15 | }); 16 | afterEach((): void => { 17 | delete (global as any).requestAnimationFrame; 18 | }); 19 | 20 | it("called on animation frame", function (): void { 21 | const throttleSpy = spy(); 22 | const throttled = throttle(throttleSpy); 23 | 24 | throttled(); 25 | 26 | assert.notCalled(throttleSpy); 27 | fire(); 28 | assert.calledOnce(throttleSpy); 29 | assert.alwaysCalledWith(throttleSpy); 30 | }); 31 | 32 | it("called only once", function (): void { 33 | const throttleSpy = spy(); 34 | const throttled = throttle(throttleSpy); 35 | 36 | throttled(); 37 | throttled(); 38 | throttled(); 39 | throttled(); 40 | 41 | assert.notCalled(throttleSpy); 42 | fire(); 43 | assert.calledOnce(throttleSpy); 44 | assert.alwaysCalledWith(throttleSpy); 45 | }); 46 | 47 | it("called once on each animation frame", function (): void { 48 | const throttleSpy = spy(); 49 | const throttled = throttle(throttleSpy); 50 | 51 | throttled(); 52 | throttled(); 53 | throttled(); 54 | throttled(); 55 | assert.notCalled(throttleSpy); 56 | 57 | throttled(); 58 | throttled(); 59 | fire(); 60 | assert.calledOnce(throttleSpy); 61 | assert.alwaysCalledWith(throttleSpy); 62 | 63 | throttled(); 64 | fire(); 65 | assert.calledTwice(throttleSpy); 66 | assert.alwaysCalledWith(throttleSpy); 67 | 68 | throttled(); 69 | throttled(); 70 | throttled(); 71 | fire(); 72 | assert.calledThrice(throttleSpy); 73 | assert.alwaysCalledWith(throttleSpy); 74 | }); 75 | 76 | it("called only if requested before the animation frame", function (): void { 77 | const throttleSpy = spy(); 78 | const throttled = throttle(throttleSpy); 79 | 80 | throttled(); 81 | throttled(); 82 | assert.notCalled(throttleSpy); 83 | 84 | throttled(); 85 | fire(); 86 | assert.calledOnce(throttleSpy); 87 | assert.alwaysCalledWith(throttleSpy); 88 | 89 | fire(); 90 | assert.calledOnce(throttleSpy); 91 | assert.alwaysCalledWith(throttleSpy); 92 | 93 | throttled(); 94 | throttled(); 95 | fire(); 96 | assert.calledTwice(throttleSpy); 97 | assert.alwaysCalledWith(throttleSpy); 98 | }); 99 | }); 100 | -------------------------------------------------------------------------------- /test/top-most.test.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | 3 | import { topMost } from "../src"; 4 | 5 | describe("topMost", function (): void { 6 | it("Single level, first object", function (): void { 7 | const pile = [ 8 | { theValue: "It‘s me :-)." }, 9 | { theValue: "Nobody cares about me :-(." }, 10 | ]; 11 | 12 | expect(topMost(pile, ["theValue"])).to.equal("It‘s me :-)."); 13 | expect( 14 | topMost(pile, "theValue"), 15 | "String accessor should be accepted too." 16 | ).to.equal("It‘s me :-)."); 17 | }); 18 | 19 | it("Single level, middle object", function (): void { 20 | const pile = [ 21 | { foo: "Move along, I don‘t have it." }, 22 | { theValue: "It‘s me :-)." }, 23 | { theValue: "Nobody cares about me :-(." }, 24 | ]; 25 | 26 | expect(topMost(pile, ["theValue"])).to.equal("It‘s me :-)."); 27 | expect( 28 | topMost(pile, "theValue"), 29 | "String accessor should be accepted too." 30 | ).to.equal("It‘s me :-)."); 31 | }); 32 | 33 | it("Nested objects", function (): void { 34 | const pile = [ 35 | {}, 36 | { foo: {} }, 37 | { foo: { bar: {} } }, 38 | { foo: { bar: { theValue: "It‘s finally me :-)." } } }, 39 | { foo: { bar: { theValue: "Nobody cares about me :-(." } } }, 40 | ]; 41 | 42 | expect(topMost(pile, ["foo", "bar", "theValue"])).to.equal( 43 | "It‘s finally me :-)." 44 | ); 45 | }); 46 | 47 | it.skip("Nested objects and primitives", function (): void { 48 | // @TODO: This doesn't work, but I think it should work. 49 | // Any other opinions about it? 50 | const pile = [ 51 | undefined, 52 | null, 53 | true, 54 | {}, 55 | { foo: null }, 56 | { foo: {} }, 57 | { foo: { bar: 77 } }, 58 | { foo: { bar: {} } }, 59 | { foo: { bar: { theValue: "It‘s finally me :-)." } } }, 60 | { foo: { bar: { theValue: "Nobody cares about me :-(." } } }, 61 | ]; 62 | 63 | expect(topMost(pile, ["foo", "bar", "theValue"])).to.equal( 64 | "It‘s finally me :-)." 65 | ); 66 | }); 67 | }); 68 | -------------------------------------------------------------------------------- /tsconfig.check.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "exclude": ["**/*.errors.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.code.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "compilerOptions": { 4 | "declaration": false, 5 | "declarationMap": false 6 | }, 7 | "exclude": ["test"] 8 | } 9 | -------------------------------------------------------------------------------- /tsconfig.docs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "compilerOptions": { 4 | "noEmit": true 5 | }, 6 | "exclude": ["test"] 7 | } 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "declaration": true, 5 | "declarationDir": "dist/types", 6 | "declarationMap": true, 7 | "esModuleInterop": true, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "outDir": "dist/src", 11 | "paths": {}, 12 | "resolveJsonModule": true, 13 | "sourceMap": true, 14 | "strict": true, 15 | "target": "esnext" 16 | }, 17 | "exclude": ["node_modules", "**/__tests__/*"], 18 | "include": ["src", "test"] 19 | } 20 | -------------------------------------------------------------------------------- /tsconfig.lint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "include": ["*.js", "*.ts", ".*.js", ".*.ts", "dev-lib", "src", "test"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.types.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "compilerOptions": { 4 | "declarationDir": "declarations", 5 | "emitDeclarationOnly": true 6 | }, 7 | "exclude": ["test"] 8 | } 9 | -------------------------------------------------------------------------------- /typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "entryPoints": ["./src/index.ts"], 3 | "excludeExternals": true, 4 | "excludePrivate": true, 5 | "out": "./docs", 6 | "theme": "default", 7 | "tsconfig": "./tsconfig.docs.json", 8 | "validation": { 9 | "invalidLink": true, 10 | "notDocumented": false, 11 | "notExported": true 12 | } 13 | } 14 | --------------------------------------------------------------------------------