immutable-js | Immutable persistent data collections for Javascript | Dataset library
kandi X-RAY | immutable-js Summary
kandi X-RAY | immutable-js Summary
Immutable data cannot be changed once created, leading to much simpler application development, no defensive copying, and enabling advanced memoization and change detection techniques with simple logic. Persistent data presents a mutative API which does not update the data in-place, but instead always yields new updated data. Immutable.js provides many Persistent Immutable data structures including: List, Stack, Map, OrderedMap, Set, OrderedSet and Record. These data structures are highly efficient on modern JavaScript VMs by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data. Immutable.js also provides a lazy Seq, allowing efficient chaining of collection methods like map and filter without creating intermediate representations. Create some Seq with Range and Repeat.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Set bounds of range .
- Creates a new slice from the current collection .
- Create a hash for the JS object .
- Iterate over a list .
- ZIP iterator generator
- Flattens a sequence
- PURE_IMPORTS_START _util_iterator PURE_IMPORTS_END
- Concatenates a collection and returns a new array with the values that are querying .
- Updates the ordered map with the given value .
- Updates the index of the given node in the list .
immutable-js Key Features
immutable-js Examples and Code Snippets
CREATE OR REPLACE FUNCTION clean_column(INOUT _text text)
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS
$func$
DECLARE
_reg CONSTANT text[] := ARRAY[
['search','replace']
, ['search','replace']
, ['search','repl
CREATE OR REPLACE FUNCTION public.fun(j json)
RETURNS text
LANGUAGE sql IMMUTABLE PARALLEL SAFE AS
$func$
SELECT string_agg(elem ->> 'insert', ' ')
FROM json_array_elements(j -> 'ops') elem
WHERE json_typeof(elem->'inser
ALTER TABLE table_name ALTER COLUMN col TYPE UUID USING uuid_or_null(col);
delete from table_name where col is null;
create function uuid_or_null(s text) returns uuid immutable AS
$$
begin
return s::uuid;
excepti
create or replace function timestamp_set(unit text, tstamp timestamp, num int)
returns timestamp language sql immutable as $$
select tstamp+ (num- date_part(unit, tstamp))* format('1 %s', unit)::interval
$$;
se
> (define (g) '(1 2 3))
> (g)
(1 2 3)
> (set-car! (g) 0)
ERROR on line 3: set-car!: immutable pair: (1 2 3)
CREATE UNIQUE INDEX tbl_row_uni ON tbl (hash_record_extended(tbl.*,0));
CREATE UNIQUE INDEX tbl_row_uni ON tbl (hash_record_extended(tbl,0));
CREATE OR REPLACE FUNCTION public.f_tbl_bighash
{
dir: "foo",
sub_dir: { ... },
sub_dir: { ... }, // cannot reuse "sub_dir" key
}
{
"test1":
{
"test2":
{
"test3":
{
"0001.dcm": true,
CREATE OR REPLACE FUNCTION regex(NAME VARCHAR)
RETURNS string
LANGUAGE javascript
STRICT IMMUTABLE AS
$$
const regex = /[[]\s{0,5}v\s{0,5}=\s{0,5}([0-9]+)/i;
let s = NAME.match(regex);
if (s != null) {
return s[0].spli
create or replace function REPLACE_DIACRITICS("str" string)
returns string
language javascript
strict immutable
as
$$
return str.normalize("NFD").replace(/\p{Diacritic}/gu, "");
$$;
select REPLACE_DIACRITICS('ö, é, č => a, o e, c')
SELECT count(*) FILTER (WHERE source = 'Netflix') * 100.0
/ count(*) AS visitors_percentage
FROM visits.visitors_log
WHERE day >= timestamp '2019-12-22' AT TIME ZONE 'PST'
AND day < timestamp '2019-12-23' AT TIME ZONE 'PS
Community Discussions
Trending Discussions on immutable-js
QUESTION
With the release of Immutable v4, there has been some api changes.
toJS(): any
became toJS(): {[key: string]: unknown}
From what I understand it has been replaced by toJSON()
but what's the difference with toObject()
? The documentation isn't very clear on that topic.
ANSWER
Answered 2022-Mar-24 at 13:20The names can be taken literally:
toObject
creates javascript plain objects, while toJSON
chooses the datatype based on the collection type.
For keyed collections (Record, Map, OrderedMap), toJSON and toObject do the same.
However, for list types (Lists, Set, OrderedSet) there is a difference:
toJSON
returns an Array, while toObject
generates an Object where the keys are the indexes:
QUESTION
I got this typing error when trying to use immutable js reviver from fromJS
function.
Check this TS playground, I could reproduce it there.
...ANSWER
Answered 2021-Nov-02 at 23:10Not sure if this breaks any of your functionality as I have little context of exactly what you are trying to do. But you can get rid of the error by saying that you want key of user in fromJS, like this:
QUESTION
In my project(Next.js v10), the immutable library is used to work with redux. Now I tackled the issue of optimization, because I ran into the problem of "red" first load js.
I am not very strong in this yet, but I try to learn and understand everything. I applied dynamic import on the pages themselves, as it is advised everywhere, and it helped a lot, since the situation was even worse than now. I checked _document.js and _app.js, everything seems to be fine except:
...ANSWER
Answered 2021-Aug-20 at 09:09I published the results of my work, I hope this will help someone(Sorry for my English :)).
Ditching immutable.js in favor of Immer did make sense (156 => 123):
Also, if anyone is interested, take a closer look at your chunks. As you can see from my question, in addition to Immutable, http-status.js was also "added" to the general First Load JS. This is a standard file with a set of response codes, from which I needed only one (I just wrote the number manually and removed the import), and the file where it was imported is distributed to the entire application. Additionally, I revised the connection of third-party scripts and used the internal optimization of fonts in next v10:
Also, in conjunction with immutable, json-immutable was used, which is no longer required, which removed 2 more small chunks.
And my previously problematic chunk now looks like this:
Finally: "First Load JS shared by all" has been reduced from 156 kB to 111kB (28.85%)
P.S. I have such a big _app.js chunk because I have Automatic Static Optimization disabled due to getInitialProps
QUESTION
been banging my head against this for hours.
I am trying to remove a nested elment of an array in an object. It must be done immutably within a reducer, which is making it tricky.
Here is the object:
...ANSWER
Answered 2021-Apr-28 at 06:26First of clone your questions
object by using ...
ES6 operator
const clonedQuestion = {...questions}
Now for removing element from answers array
QUESTION
Raku provides many types that are immutable and thus cannot be modified after they are created. Until I started looking into this area recently, my understanding was that these Types were not persistent data structures – that is, unlike the core types in Clojure or Haskell, my belief was that Raku's immutable types did not take advantage of structural sharing to allow for inexpensive copies. I thought that statement my List $new = (|$old-list, 42);
literally copied the values in $old-list
, without the data-sharing features of persistent data structures.
That description of my understanding is in the past tense, however, due to the following code:
...ANSWER
Answered 2021-Apr-10 at 15:02I remember implementing these semantics, and I certainly don't recall thinking about them giving rise to a persistent data structure at the time - although it does seems fair to attach that label to the result!
I don't think you'll find anywhere that explicitly spells out this exact behavior, however the most natural implementation of things that are required by the language quite naturally leads to it. Taking the ingredients:
- The
infix:<,>
operator is theList
constructor in Raku - When a
List
is created, it is non-committal with regards to laziness and flattening (these arise from how we use theList
, which we don't - in general - know at the point of its construction) - When we write
(|$x, 1)
, theprefix:<|>
operator constructs aSlip
, which is a kind ofList
that should melt into its surroundingList
. Thus whatinfix:<,>
sees is aSlip
and anInt
. - Making the
Slip
melt into the resultList
immediately would mean making a commitment about eagerness, whichList
construction alone should not do. Thus theSlip
and everything after it is placed into the lazily evaluated ("non-reified") portion of theList
.
This last of these is what gives rise to the observed persistent data structure style behavior.
I expect it would be possible to have a implementation that inspects the Slip
and chooses to eagerly copy things that are known not to be lazy, and still be in compliance with the specification test suite. That would change the time complexity of your example. If you want to be defensive against that, then:
QUESTION
I'm trying to create a wrapper function that takes in a query and a reducer, then returns essentially the same function, but with an additional property (_data
) that contains a processed version of the query data (specifically, I want to cast the query result to an Immutable.js record).
I'd like this function to be generic over any possible query, and preserve the call signature and type information from the query. I've attempted to do this by using Parameters
and ReturnType
. However, when I pass the arguments to queryHook
in the inner function, Typescript complains that the arguments aren't assignable to the inner function, even though they should be typed from that function's parameters.
My question is, why can't Typescript tell that the arguments being passed to the wrapped query should be of identical type to the the query's expected arguments?
Currently, it gives an error that suggests that the queryHook
could be any of the possible queries, not the specific one that was passed to the function.
Weirdly, if I actually use the function, all types are inferred correctly...
My code is as follows:
...ANSWER
Answered 2020-Oct-18 at 00:30Instead of trying to work backwards based on the signatures of two particular hooks, let's try to make a more generalized function. Typescript should be able to infer the specific types based on which hook you pass in as an argument.
Start by thinking about what we know and what we don't know. The things that we don't know become the generics.
- A hook is a function which takes some arguments (
A
). There can be any number of arguments of any type (A extends any[]
). - It returns some object (
R
) which might have a property called 'data' (R extends { data?: any }
). - A reducer is a function which takes the data returned by the hook (
R['data']
) and maps it to some new value (M
). - The modified hook takes the same arguments (
A
) and returns an object with all of the hook returned values (R
) and an additional property '_data' whose value is returned from the reducer (M
) or might be undefined (R & { _data: M | undefined }
).
Put that all together and we get this:
QUESTION
There is an Immutable.js with the structural data as collections. Let's take a Map. Also there are methods to work with:
- includes
- filter
Let's consider these data:
...ANSWER
Answered 2020-Oct-15 at 09:37As you pointed out, the semantics are slightly different. In the example case they both provide an intersection of the ids, so
QUESTION
I'm studying about record of immutable.js.
but this code almost kill me.
My Question
- I know [import, export,const] but [type] is what mean.
defaultValues:
,makePoint3D:
,getName(): string
,setName(name: string): this
what mean. I never seen:
unless in object or if .
This question is key for my understanding.
please give me advice!
...ANSWER
Answered 2020-Jul-01 at 09:11getName(): string
, getName()
is your method and part after :
tells about return type. Basically part after :
usually denotes about type . return type , variable type etc whatever you are using
QUESTION
Is this a good solution? https://github.com/immutable-js/immutable-js/wiki/Immutable-as-React-state
I need to put nested objects in my state. This solution seems simple enough in code, but I'm wondering if there are any reasons not to take this approach.
...ANSWER
Answered 2020-Jun-11 at 20:41TL/DR: I recommend immutable for anything bigger than a simple news-list, together with redux-saga. But I would and don't use it for component's local state.
We have a big React app (JS, not TypeScript) that has all the state as Immutable objects. The recommendation usually is the same as with redux and redux-saga: If you don't know why you need it, you probably don't. But here is the thing, if you start without it and later notice that it would be helpful, you are already deep in a mess and it's hard to switch over.
Immutable doesn't add obvious business value, but it reduces the chances of bugs and prevents you from doing things that might look good now but in the long run increase the cost of development. Especially when your dev team isn't an experienced bunch of seniors or generally tends to not strictly follow rules.
However, in my opinion, it is unecessary overkill for an components internal state, at least if business state is stored in your global redux state.
PS: Immutable is mature and probably doesn't need patches much, but it has to be said: Development of immutable essentially is stopped, they are discussing to bring in new people but who knows how that turns out. There are similar libraries which are more active
QUESTION
I am using Typescript version 3.8.3
with immutable@4.0.0-rc.12
.
When I create OrderedMap
using two-dimensional array to instantiate it, I get error from Typescript.
ANSWER
Answered 2020-Apr-22 at 09:05First of all, @types/immutable
is no longer supported
This package has been deprecated
Author message:
This is a stub types definition for Facebook's Immutable (https://github.com/facebook/immutable-js). Facebook's Immutable provides its own type definitions, so you don't need @types/immutable installed!
After removing it, you should get nice type hints in your editor.
Working example: https://stackblitz.com/edit/typescript-vwetmv?file=index.ts
Update
Adding "target": "es2015"
in compiler options removes the error.
See docs: Using TypeScript with Immutable.js v4
Immutable.js type definitions embrace ES2015. While Immutable.js itself supports legacy browsers and environments, its type definitions require TypeScript's 2015 lib. Include either
"target": "es2015"
or"lib": "es2015"
in yourtsconfig.json
, or provide--target es2015
or--lib es2015
to thetsc
command.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install immutable-js
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page