Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Entries from 2017-04-01 to 1 month

Meanings of :owner, :repo, ... in GitHub API document

In GitHub API document (ex. https://developer.github.com/v3/repos/ ), examples of how to access API showed like GET /repos/:owner/:repo/contributors. Meanings of indicator variable-like valiables such as :owner, :repo are listed below: # v…

How to convert string to number with AngularJS

There is more than one way to convert string to number in JavaScript. Context: Use AngularJS to bind text boxes to variables. Sum up input values in some text boxes. Omit ng-required, ng-pattern, ng-minlength, ng-maxlength, etc., wherever …

URL of GitHub Enterprise API is different from github.com's one

Problem: I have tried to access GitHub Enterprise API with a URL like showed below, based on examples from some websites, but the response was 404 error. https://x.x.x.x/repos/Project/Repository/git/refs/heads Reason: URL of GitHub Enterpr…

System.Text.Encoding.GetEncodings() does not show all available encodings after call RegisterProvider()

Problem: If you want to use character encodings other than the default registered encodings, you have to call [System.Text.Encoding]::RegisterProvider([System.Text.CodePagesEncodingProvider]::Instance). But even if you call that method, th…

How to convert from a code point (U+xxxx) to a code point in another character encoding

function Convert-CodePoint { Param( [Parameter(ValueFromPipeline=$true,Mandatory=$true)] [string] $CodePoint, [Parameter(ValueFromPipeline=$false,Mandatory=$true)] $From, [Parameter(ValueFromPipeline=$false,Mandatory=$true)] $To ) begin { …

What does "US-ASCII only" means in Java regexp?

Java Patten class (regexp) supports POSIX character classes like \p{XDigit}. They are very useful when you want to check hex strings. In Java API Document, POSIX character classes say (US-ASCII only). What does it mean? https://docs.oracle…

Trimmed characters by trim() are different between languages

String object in .NET (PowerShell), JavaScript, and Java all have trim() method. But trimmed characters by trim() are different between these languages. # codepoint .NET JavaScript Java 1 0000-0008 ✘ ✘ ✔ 2 0009-000D ✔ ✔ ✔ 3 000E-001F ✘ ✘ ✔…

How to convert from a code point (U+xxxx) to a character

function ConvertFrom-CodePoint { Param( [Parameter(ValueFromPipeline=$true,Mandatory=$true)] [string] $CodePoint, [Parameter(ValueFromPipeline=$false,Mandatory=$true)] $From ) begin { [System.Text.Encoding]::RegisterProvider([System.Text.C…

You can't use > in comparation (and it will be silently failed)

In PowerShell, you have to use -lt and -gt, instead of < and > in comparation, like POSIX sh. Especially if you wrongly use > like below, the comparison will be silently failed (evaluated as $false) and value of left-hand operand is wrote …

Escape "*" in -like and -match

When you use -like, escape character to escape “*” depends on quotation marks. # quotation escape command Result 1 “ N/A $_ -like “*” Not escaped / Match to any strings 2 “ ` $_ -like “`*” Not escaped / Match to any strings 3 “ `` $_ -like…

You cannot set a result of cmdlets to a property of object by Set-Variable

Context: You can set a result of cmdlets to a variable by Set-Variable. Problem: You cannot set a result of cmdlets to a property of object by Set-Variable. PS > $foo = New-Object PSObject | Select-Object Property PS > Get-ChildItem | Set-…

Mandatory parameter from pipeline, and empty lines

In a function that retrieve parameters from pipeline, if you make a parameter mandatory (Mandatory=$true), you will get error when an empty line is passed from the pipeline. If you receive parameters from the data which may contain empty l…

You cannot retrieve a required parameter from "either pipeline or argument"

## Context: A mandatory parameter of a function retrieved from pipeline is defined like this: param( [Parameter(ValueFromPipeline=$true, Mandatory=$true)] [string] $foo ) A mandatory parameter of a function retrieved from argument is defin…

ConvertTo-Csv or Export-Csv always add double quote to all columns

ConvertTo-Csv or Export-Csv always add double quote to all columns. Get-Help Export-Csv -Full shows samples of CSV with no double quotes, but it seems there are no options to suppress double quote. This behavior doesn’t depend on system lo…

Avoid error when adding duplicated keys to hash (case sensitivity in hash tables on PowerShell)

Problem: Keys of hash tables are case sensitive in most languages (Java/JavaScript/Perl/Ruby/…). But in PowerShell, key of hash table is case insensitive (if you use string as key), so you will bump into errors in code like: PS > $hash = @…

You can't sort strings in ASCII order

When you pass chars to Sort-Object, the chars sorted in ASCII order. PS > (0x7E..0x21 | ForEach-Object { [char]$_ } | Sort-Object) -join '' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ But …

Difference of Sort-Object -Unique, Sort-Object | Get-Unique, and Select-Object -Unique

If you want to uniq a list in PowerShell, there is more than one way to do it and these are slightly different. # Cmdlets Sort-Object -Unique Sort-Object | Get-Unique Select-Object -Unique 1 Sort elements Yes Yes No 2 Case sensitive in ded…